데이터베이스 내에 프로시저를 선언하며 클라이언트에서 저장된 프로시저를 실행만 해주면 그 프로시저 내용이 처리된다
트랜잭션은 데이터 처리의 한 단위
오라클 서버에서 발생하는 SQL문들을 하나의 논리적인 작업단위로써 성공하거나 실패하는
일련의 SQL문을 트랜잭션이라고 할 수 있음
트랜잭션은 데이터를 일관되게 변경하는 DML문장으로 구성됨
실행 가능한 SQL문장이 제일 처음 실행될 때
package kr.s07.note;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import kr.s03.preparedstatement.DBUtil;
/*
* DAO : Data Access Object
* 데이터베이스의 데이터를 전문적으로 호출하고 제어하는 객체
*/
public class NoteDAO {
//글쓰기
public void insertInfo(String name,String passwd,String subject,
String content,String email) {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = null;
try {
//JDBC 수행 1,2단계
conn = DBUtil.getConnection();
//SQL문 작성
sql = "INSERT INTO note VALUES (note_seq.nextval,"
+ "?,?,?,?,?,SYSDATE)";
//JDBC 수행 3단계
pstmt = conn.prepareStatement(sql);
//?에 데이터 바인딩
pstmt.setString(1, name);
pstmt.setString(2, passwd);
pstmt.setString(3, subject);
pstmt.setString(4, content);
pstmt.setString(5, email);
//JDBC 수행 4단계 : SQL문을 실행해서 테이블에 행을 추가
int count = pstmt.executeUpdate();
System.out.println(count + "개의 행을 추가했습니다.");
}catch(Exception e) {
e.printStackTrace();
}finally {
//자원정리
DBUtil.executeClose(null, pstmt, conn);
}
}
//목록보기
public void selectInfo() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
try {
//JDBC 수행 1,2단계
conn = DBUtil.getConnection();
//SQL문 작성
sql = "SELECT * FROM note ORDER BY num DESC";
//JDBC 수행 3단계
pstmt = conn.prepareStatement(sql);
//JDBC 수행 4단계
rs = pstmt.executeQuery();
System.out.println("---------------------------------------");
System.out.println("글번호\t이름\t작성일\t\t제목");
while(rs.next()) {
System.out.print(rs.getInt("num") + "\t");
System.out.print(rs.getString("name") + "\t");
System.out.print(rs.getDate("reg_date") + "\t");
System.out.println(rs.getString("subject"));
}
System.out.println("---------------------------------------");
}catch(Exception e) {
e.printStackTrace();
}finally {
//자원정리
DBUtil.executeClose(rs, pstmt, conn);
}
}
//상세글보기
public void selectDetailInfo(int num) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = null;
try {
//JDBC 수행 1,2단계
conn = DBUtil.getConnection();
//SQL문 작성
sql = "SELECT * FROM note WHERE num=?";
//JDBC 수행 3단계
pstmt = conn.prepareStatement(sql);
//?에 데이터 바인딩
pstmt.setInt(1, num);
//JDBC 수행 4단계
rs = pstmt.executeQuery();
if(rs.next()) {
System.out.println("글번호 : " + rs.getInt("num"));
System.out.println("이름 : " + rs.getString("name"));
System.out.println("비밀번호 : " + rs.getString("passwd"));
System.out.println("제목 : " + rs.getString("subject"));
System.out.println("내용 : " + rs.getString("content"));
System.out.println("이메일 : " + rs.getString("email"));
System.out.println("작성일 : " + rs.getDate("reg_date"));
}else {
System.out.println("검색된 정보가 없습니다.");
}
}catch(Exception e) {
e.printStackTrace();
}finally {
//자원정리
DBUtil.executeClose(rs, pstmt, conn);
}
}
//글수정
public void updateInfo(int num,String name,String passwd,
String subject,String content,String email) {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = null;
try {
//JDBC 수행 1,2단계
conn = DBUtil.getConnection();
//SQL문 작성
sql = "UPDATE note SET name=?,passwd=?,subject=?,"
+ "content=?,email=? WHERE num=?";
//JDBC 수행 3단계
pstmt = conn.prepareStatement(sql);
//?에 데이터 바인딩
pstmt.setString(1, name);
pstmt.setString(2, passwd);
pstmt.setString(3, subject);
pstmt.setString(4, content);
pstmt.setString(5, email);
pstmt.setInt(6, num);
//JDBC 수행 4단계
int count = pstmt.executeUpdate();
System.out.println(count + "개의 행을 수정했습니다.");
}catch(Exception e) {
e.printStackTrace();
}finally {
//자원정리
DBUtil.executeClose(null, pstmt, conn);
}
}
//글삭제
public void deleteInfo(int num) {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = null;
try {
//JDBC 수행 1,2단계
conn = DBUtil.getConnection();
//SQL문 작성
sql = "DELETE FROM note WHERE num=?";
//JDBC 수행 3단계
pstmt = conn.prepareStatement(sql);
//?에 데이터 바인딩
pstmt.setInt(1, num);
//JDBC 수행 4단계
int count = pstmt.executeUpdate();
System.out.println(count + "개의 행을 삭제했습니다.");
}catch(Exception e) {
e.printStackTrace();
}finally {
//자원정리
DBUtil.executeClose(null, pstmt, conn);
}
}
}
package kr.s07.note;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class NoteMain {
private BufferedReader br;
private NoteDAO note;
public NoteMain() {
note = new NoteDAO();
try {
br = new BufferedReader(new InputStreamReader(System.in));
callMenu();
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}finally {
//자원정리
if(br!=null)try {br.close();}catch(IOException e) {}
}
}
//메뉴
public void callMenu()throws IOException{
while(true) {
System.out.print(
"1.글쓰기,2.목록보기,3.상세글보기,4.글수정,5.글삭제,6.종료>");
try {
int no = Integer.parseInt(br.readLine());
if(no == 1) {
//글쓰기
System.out.print("이름:");
String name = br.readLine();
System.out.print("비밀번호:");
String passwd = br.readLine();
System.out.print("제목:");
String subject = br.readLine();
System.out.print("내용:");
String content = br.readLine();
System.out.print("이메일:");
String email = br.readLine();
//NoteDAO의 insertInfo 메서드를 호출해서
//입력받은 데이터 전달
note.insertInfo(name, passwd, subject,
content, email);
}else if(no == 2) {
//목록보기
note.selectInfo();
}else if(no == 3) {
//상세글보기
note.selectInfo();
System.out.print("선택한 글의 번호:");
int num = Integer.parseInt(br.readLine());
note.selectDetailInfo(num);
}else if(no == 4) {
//글수정
note.selectInfo();
System.out.print("수정할 글의 번호:");
int num = Integer.parseInt(br.readLine());
System.out.print("이름:");
String name = br.readLine();
System.out.print("비밀번호:");
String passwd = br.readLine();
System.out.print("제목:");
String subject = br.readLine();
System.out.print("내용:");
String content = br.readLine();
System.out.print("이메일:");
String email = br.readLine();
note.updateInfo(num, name, passwd, subject,
content, email);
}else if(no == 5) {
//글삭제
note.selectInfo();
System.out.print("삭제할 글의 번호:");
int del_num = Integer.parseInt(br.readLine());
note.deleteInfo(del_num);
}else if(no == 6) {
//종료
System.out.println("프로그램을 종료합니다.");
break;
}else {
System.out.println("잘못 입력했습니다.");
}
}catch(NumberFormatException e) {
System.out.println("숫자만 입력 가능!!");
}
}
}
public static void main(String[] args) {
new NoteMain();
}
}
1.글쓰기,2.목록보기,3.상세글보기,4.글수정,5.글삭제,6.종료>1
이름:가나다
비밀번호:123
제목:test
내용:null
이메일:test@test.com
1개의 행을 추가했습니다.