본문 바로가기
프로그래밍/자바

BookDAO

by 카라미 2016. 1. 14.

package jdbcExam;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;


public class BookDAO {

/*

*/

public int addBook(String bookTitle, int page, String author, String content){

int insertCount = 0;

Connection conn = null;

PreparedStatement ps = null;

try{

//3. 드라이버로딩

Class.forName("oracle.jdbc.driver.OracleDriver");

//4.디비접속

String url="jdbc:oracle:thin:@127.0.0.1:1521:xe";

String user="smu";

String password = "smu";

conn = DriverManager.getConnection(url, user, password);

//5. 쿼리작성

String sql = "insert into book values(book_seq.nextval,?,?,?,?,sysdate)";

ps = conn.prepareStatement(sql);

ps.setString(1, bookTitle);

ps.setInt(2, page);

ps.setString(3, author);

ps.setString(4, content);

insertCount = ps.executeUpdate();

}catch(Exception e){

e.printStackTrace();

}finally {

//2. 닫는다. 

if(ps != null){

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(conn != null){

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

return insertCount;

}

public void delBook(int bookId){

//1. 선언 (필요한 객체 선언) 

Connection conn = null;

PreparedStatement ps = null;

try{

//3. 드라이버로딩

Class.forName("oracle.jdbc.driver.OracleDriver");

//4.디비접속

String url="jdbc:oracle:thin:@127.0.0.1:1521:xe";

String user="smu";

String password = "smu";

conn = DriverManager.getConnection(url, user, password);

//5. 쿼리작성

String sql = "delete book where book_id=?";

ps = conn.prepareStatement(sql);

ps.setInt(1, bookId);

//6. 실행 

ps.executeUpdate();

}catch(Exception e){

e.printStackTrace();

}finally {

//2. 닫는다. 

if(ps != null){

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(conn != null){

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}



'프로그래밍 > 자바' 카테고리의 다른 글

BookDAO , BookVO  (0) 2016.01.14
DBUtil 이용하기  (0) 2016.01.14
수업중 기본명령  (0) 2016.01.13
String&io 연습문제  (0) 2016.01.12
아이디 자동생성기  (0) 2016.01.11