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<BookPO> findAll(){
		try{
			List<BookPO> bookList = new ArrayList<BookPO>();
			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<BookPO> findByIdOrName(String id, String name){
		StringBuffer sql = new StringBuffer();
		List<String> paramList = new ArrayList<String>();
		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<BookPO> bookList = new ArrayList<BookPO>();
			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<BookPO> bookList = new ArrayList<BookPO>();
//			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;
//	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值