jdbc 연결 oralce 작업
package com.dgh.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.dgh.model.BookPO;
import com.dgh.model.UserPO;
public class DBUtil {
Connection conn; //
PreparedStatement pstmt; //
ResultSet rs; //
//
public DBUtil(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:dbname",
"userName", "password");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
}
//
public void closeConn(){
try{
if(rs != null){
rs.close();
}
}catch (Exception e) {}
try{
if(pstmt != null){
pstmt.close();
}
}catch (Exception e) {}
try{
if(conn != null){
conn.close();
}
}catch (Exception e) {}
}
// userName , , null
public UserPO checkUser(String userName){
try {
String sql = "select * from t_user where userName = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userName);
rs = pstmt.executeQuery();
UserPO user = new UserPO();
while(rs.next()){
//UserPO.setId(rs.getString("Id"));
//UserPO.setUserName(rs.getString("userName"));
//UserPO.setPassword(rs.getString("password"));
return user;
}
return null;
} catch (Exception e) {
e.printStackTrace();
} finally{
closeConn();
}
return null;
}
// userName, , , null
public UserPO login(String userName, String password){
UserPO userPO = null;
userPO = checkUser(userName);
if(userPO != null && password.equals(userPO.getPassword())){
return userPO;
}else{
return null;
}
}
//
public List findAll(){
try{
List bookList = new ArrayList();
pstmt = conn.prepareStatement("select * from t_book");
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
BookPO bookPO = new BookPO();
//bookPO.setId(rs.getString("id"));
//bookPO.setBookName(rs.getString("bookName"));
//bookList.add(book);
}
return bookList;
}catch (Exception e) {
e.printStackTrace();
}finally{
closeConn();
}
return null;
}
// id
public int findBookNum(String id){
try {
pstmt = conn.prepareStatement("select * from t_book where id = ?");
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
return rs.getInt("amount");
}
} catch (Exception e) {
e.printStackTrace();
} finally{
closeConn();
}
return 0;
}
// id ,
public int lendBook(String id){
try{
pstmt = conn.prepareStatement("update t_book set num = num - 1 where id = ?");
pstmt.setString(1, id);
int count = pstmt.executeUpdate();
return count;
}catch (Exception e) {
e.printStackTrace();
}finally{
closeConn();
}
return 0;
}
// id
public List findByIdOrName(String id, String name){
StringBuffer sql = new StringBuffer();
List paramList = new ArrayList();
sql.append("select * from t_book where 1 = 1 ");
// , sql
if(id != null){
sql.append(" and id like ? ");
paramList.add(id);
}
if(name != null){
sql.append(" and name linke ? ");
paramList.add(name);
}
try{
pstmt = conn.prepareStatement(sql.toString());
for (int i = 0; i < paramList.size(); i++) {
pstmt.setString(i+1, "%"+paramList.get(i)+"%");
}
//
ResultSet rs = pstmt.executeQuery();
List bookList = new ArrayList();
while (rs.next()){
BookPO bookPO = new BookPO();
//bookPO.setId(rs.getString("id"));
//bookPO.setBookName(rs.getString("bookName"));
//bookList.add(book);
}
return bookList;
} catch (Exception e) {
e.printStackTrace();
} finally{
closeConn();
}
return null;
}
// id id
public int giveBackBook(String userId, String bookId){
try{
pstmt = conn.prepareStatement("select * from t_book b,t_user u,t_book_uer bu where ");
pstmt.executeQuery();
return 0;
//return bookList;
}catch (Exception e) {
e.printStackTrace();
}finally{
closeConn();
}
return 0;
}
// // id
// public List findByUserId(){
// try{
// List bookList = new ArrayList();
// pstmt = conn.prepareStatement("select * from t_book b,t_user u,t_book_uer bu where ");
// ResultSet rs = pstmt.executeQuery();
// while (rs.next()){
// BookPO bookPO = new BookPO();
// //bookPO.setId(rs.getString("id"));
// //bookPO.setBookName(rs.getString("bookName"));
// //bookList.add(book);
// }
// return bookList;
// }catch (Exception e) {
// e.printStackTrace();
// }finally{
// closeConn();
// }
// return null;
// }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.