一、示例说明
定义StudentDaoImpl业务原子性数据DAO访问层增删改查,通过DBUtil工具类实现java.sql.DriverManager对数据库创建连接java.sql.Connection对象,并通过java.sql.PreparedStatement对Sql语句进行executeQuery查询、executeUpdate更新处理。下载完整源码示例《亿级流量Jαva高并发与网络编程实战》的ch14章节目录。
二、代码示例
1)DBUtil类
package org.student.util;@b@@b@import java.sql.Connection;@b@import java.sql.DriverManager;@b@import java.sql.PreparedStatement;@b@import java.sql.ResultSet;@b@import java.sql.SQLException;@b@import java.sql.Statement;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@import org.student.entity.Student;@b@@b@//通用的数据操作方法@b@public class DBUtil {@b@ private static final String URL ="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" ;@b@ private static final String USERNAME ="scott" ;@b@ private static final String PASSWORD ="tiger" ;@b@ public static PreparedStatement pstmt = null ;@b@ public static Connection connection = null ;@b@ public static ResultSet rs = null ; @b@ @b@ @b@ @b@ @b@ //通用的:当前页的数据集合 ,因此当前的数据 是强烈依赖于实体类,例如 显示当前页的学生, List<Student>@b@ //因此需要将此方法 写入到dao层@b@ @b@// public static List<Student> @b@ @b@ //查询总数@b@ public static int getTotalCount(String sql ) { //select count(1) from student@b@ int count = -1 ;@b@ try {@b@ pstmt = createPreParedStatement(sql, null );@b@ rs = pstmt.executeQuery() ;//88@b@ if(rs.next()) {@b@ count = rs.getInt(1) ;@b@ }@b@ @b@ } catch (ClassNotFoundException e) {@b@ e.printStackTrace();@b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ }catch (Exception e) {@b@ e.printStackTrace();@b@ }finally {@b@ closeAll(rs, pstmt, connection);@b@ }@b@ return count;@b@ }@b@ @b@ @b@ @b@ //通用的增删改@b@ public static boolean executeUpdate(String sql,Object[] params) {//{"zs",1}@b@ try { @b@ //Object[] obs = { name,age ,...,x} ; @b@// String sql = "delete from xxx where Name = ? or id = ? " ;@b@// pstmt.setInt(1,sno );@b@ //setXxx()方法的个数 依赖于 ?的个数, 而?的个数 又和 数组params的个数一致@b@ //setXxx()方法的个数 ->数组params的个数一致@b@ pstmt = createPreParedStatement(sql,params);@b@ int count = pstmt.executeUpdate() ;@b@ if(count>0)@b@ return true ;@b@ else @b@ return false ;@b@ @b@ } catch (ClassNotFoundException e) {@b@ e.printStackTrace();@b@ return false ;@b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ return false ;@b@ }catch (Exception e) {@b@ e.printStackTrace();@b@ return false ;@b@ }@b@ finally {@b@ closeAll(null,pstmt,connection);@b@ }@b@}@b@ //Statement@b@ public static void closeAll(ResultSet rs,Statement stmt,Connection connection)@b@ {@b@ try {@b@ if(rs!=null)rs.close();@b@ if(pstmt!=null)pstmt.close();@b@ if(connection!=null)connection.close();@b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ } @b@ @b@ @b@ }@b@ @b@ @b@ @b@ @b@ public static Connection getConnection() throws ClassNotFoundException, SQLException {@b@ Class.forName("oracle.jdbc.OracleDriver") ;@b@ return DriverManager.getConnection( URL,USERNAME,PASSWORD ) ;@b@ }@b@ @b@ public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws ClassNotFoundException, SQLException {@b@ pstmt = getConnection() .prepareStatement( sql) ;@b@ if(params!=null ) {@b@ for(int i=0;i<params.length;i++) {@b@ pstmt.setObject(i+1, params[i]);@b@ }@b@ }@b@ return pstmt;@b@ }@b@ @b@ //通用的查 :通用 表示 适合与 任何查询@b@ public static ResultSet executeQuery( String sql ,Object[] params) {//select xxx from xx where name=? or id=?@b@ Student student = null;@b@ @b@ List<Student> students = new ArrayList<>();@b@ try {@b@ @b@ // String sql = "select * from student" ;//select enmae ,job from xxxx where...id>3@b@ @b@ pstmt = createPreParedStatement(sql,params);@b@ rs = pstmt.executeQuery() ;@b@ return rs ;@b@ } catch (ClassNotFoundException e) {@b@ e.printStackTrace();@b@ return null ; @b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ return null ; @b@ }catch (Exception e) {@b@ e.printStackTrace();@b@ return null ; @b@ }@b@// finally {@b@// try {@b@// if(rs!=null)rs.close();@b@// if(pstmt!=null)pstmt.close();@b@// if(connection!=null)connection.close();@b@// } catch (SQLException e) {@b@// e.printStackTrace();@b@// } @b@// }@b@ }@b@ @b@ @b@ @b@ @b@ @b@}
2)StudentDaoImpl类
package org.student.dao.impl;@b@@b@import java.sql.Connection;@b@import java.sql.DriverManager;@b@import java.sql.PreparedStatement;@b@import java.sql.ResultSet;@b@import java.sql.SQLException;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@import org.student.dao.IStudentDao;@b@import org.student.entity.Student;@b@import org.student.util.DBUtil;@b@@b@//数据访问层:原子性 的增删改查@b@public class StudentDaoImpl implements IStudentDao{@b@ private final String URL ="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" ;@b@ private final String USERNAME ="scott" ;@b@ private final String PASSWORD ="tiger" ;@b@ @b@ //存在大量冗余(重复)@b@ public boolean addStudent(Student student) {//zs 23 xa @b@ String sql ="insert into student(sno,sname,sage,saddress) values(?,?,?,?) " ;@b@ Object[] params = {student.getSno(),student.getSname(),student.getSage(),student.getSaddress()};@b@ return DBUtil.executeUpdate(sql, params) ;@b@ }@b@ @b@ //根据学号修改学生: 根据sno知道待修改的人 ,把这个人 修改成student@b@ public boolean updateStudentBySno(int sno,Student student) {//3 -> zs,23,bj@b@ String sql = "update student set sname =?,sage=?,saddress=? where sno=?" ;@b@ Object[] params = {student.getSname(),student.getSage(),student.getSaddress(),sno};@b@ return DBUtil.executeUpdate(sql, params) ;@b@ }@b@ @b@ //根据学号删除学生@b@ public boolean deleteStudentBySno(int sno) {@b@ String sql = "delete from student where sno = ?" ;@b@ Object[] params = {sno} ;@b@ return DBUtil.executeUpdate(sql, params) ;@b@ }@b@ @b@ //查询全部学生(很多学生)@b@ public List<Student> queryAllStudents() {@b@ PreparedStatement pstmt = null ;@b@ Student student = null;@b@ List<Student> students = new ArrayList<>();@b@ ResultSet rs = null ;@b@ try {@b@ String sql = "select * from student" ;@b@ rs = DBUtil.executeQuery(sql, null) ;@b@// rs = pstmt.executeQuery() ;@b@ while(rs.next()) {@b@ int no = rs.getInt("sno") ;@b@ String name = rs.getString("sname") ;@b@ int age = rs.getInt("sage");@b@ String address = rs.getString("saddress") ;@b@ student = new Student(no,name,age,address);@b@ students.add(student) ;@b@ }@b@ return students ;@b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ return null ; @b@ }catch (Exception e) {@b@ e.printStackTrace();@b@ return null ; @b@ }@b@ finally {@b@ DBUtil.closeAll(rs, pstmt, DBUtil.connection);@b@// try {@b@// if(rs!=null)rs.close();@b@// if(pstmt!=null)pstmt.close();@b@// if(DBUtil.connection!=null)DBUtil.connection.close();@b@// } catch (SQLException e) {@b@// e.printStackTrace();@b@// } @b@ }@b@ }@b@ //根据姓名查询@b@ //根据年龄查询@b@ //查询此人是否存在@b@ public boolean isExist(int sno) {//true:此人存在 false:不存在@b@ return queryStudentBySno(sno)==null? false:true ;@b@ }@b@ @b@ //根据学号 查询学生@b@ public Student queryStudentBySno(int sno) {//3@b@ Student student = null;@b@ Connection connection = null ;@b@ PreparedStatement pstmt = null ;@b@ ResultSet rs = null ; @b@ try {@b@ Class.forName("oracle.jdbc.OracleDriver") ;@b@ connection = DriverManager.getConnection( URL,USERNAME,PASSWORD ) ;@b@ String sql = "select * from student where sno =? " ;@b@ pstmt = connection.prepareStatement( sql) ;@b@ pstmt.setInt(1, sno);@b@ rs = pstmt.executeQuery() ;@b@ if(rs.next()) {@b@ int no = rs.getInt("sno") ;@b@ String name = rs.getString("sname") ;@b@ int age = rs.getInt("sage");@b@ String address = rs.getString("saddress") ;@b@ student = new Student(no,name,age,address);@b@ }@b@ return student ;@b@ } catch (ClassNotFoundException e) {@b@ e.printStackTrace();@b@ return null ; @b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ return null ; @b@ }catch (Exception e) {@b@ e.printStackTrace();@b@ return null ; @b@ }@b@ finally {@b@ try {@b@ if(rs!=null)rs.close();@b@ if(pstmt!=null)pstmt.close();@b@ if(connection!=null)connection.close();@b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ } @b@ }@b@ }@b@@b@ @Override@b@ public int getTotalCount() {//查询总数据量@b@ String sql = "select count(1) from student" ;@b@ return DBUtil.getTotalCount(sql);@b@ }@b@@b@ @Override@b@ public List<Student> queryStudentsByPage(int currentPage, int pageSize) {@b@ String sql = "select *from "@b@ +"("@b@ +"select rownum r, t.* from"@b@ +"(select s.* from student s order by sno asc) t "@b@ @b@ +"where rownum<=?"@b@ +")"@b@ + "where r>=?"@b@ ;@b@ Object[] params = {currentPage*pageSize,(currentPage-1)*pageSize+1}; @b@ @b@ List<Student> students = new ArrayList<>();@b@ @b@ ResultSet rs = DBUtil.executeQuery(sql, params) ;@b@ @b@ try {@b@ while(rs.next()) {@b@ Student student = new Student(rs.getInt("sno"),rs.getString("sname"),rs.getInt("sage"),rs.getString("saddress")) ;@b@ students.add(student) ;@b@ }@b@ } catch (SQLException e) {@b@ e.printStackTrace();@b@ }catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ return students;@b@ }@b@@b@ @b@@b@ @b@}