Java实例---简单的数据库操作
源码分析
DAOFactory.java
1 package cn.ftl.mysql ; 2 public class DAOFactory { 3 public static IEmpDAO getIEmpDAOInstance() throws Exception{ 4 return new EmpDAOProxy() ; 5 } 6 }
DatabaseConnection.java
1 package cn.ftl.mysql ; 2 import java.sql.Connection ; 3 import java.sql.DriverManager ; 4 public class DatabaseConnection { 5 public static final String DBDRIVER= "com.mysql.jdbc.Driver"; 6 public static final String DBURL= "jdbc:mysql://localhost:3306/RUNOOB"; 7 public static final String DBUSER= "test"; 8 public static final String DBPASSWORD= "Changeme_123"; 9 10 private Connection conn ; 11 public DatabaseConnection() throws Exception { 12 Class.forName(DBDRIVER) ; 13 this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ; 14 } 15 public Connection getConnection(){ 16 return this.conn ; 17 } 18 public void close() throws Exception { 19 if(this.conn != null){ 20 try{ 21 this.conn.close() ; 22 }catch(Exception e){ 23 throw e ; 24 } 25 } 26 } 27 }
Emp.java
1 package cn.ftl.mysql ; 2 import java.util.Date ; 3 public class Emp { 4 private int empno ; 5 private String ename ; 6 private String job ; 7 private Date hiredate ; 8 private float sal ; 9 public void setEmpno(int empno){ 10 this.empno = empno ; 11 } 12 public void setEname(String ename){ 13 this.ename = ename ; 14 } 15 public void setJob(String job){ 16 this.job = job ; 17 } 18 public void setHiredate(Date hiredate){ 19 this.hiredate = hiredate ; 20 } 21 public void setSal(float sal){ 22 this.sal = sal ; 23 } 24 public int getEmpno(){ 25 return this.empno ; 26 } 27 public String getEname(){ 28 return this.ename ; 29 } 30 public String getJob(){ 31 return this.job ; 32 } 33 public Date getHiredate(){ 34 return this.hiredate ; 35 } 36 public float getSal(){ 37 return this.sal ; 38 } 39 }
EmpDAOImpl.java
1 package cn.ftl.mysql; 2 3 import java.util.* ; 4 import java.sql.* ; 5 6 public class EmpDAOImpl implements IEmpDAO { 7 private Connection conn = null ; 8 private PreparedStatement pstmt = null ; 9 public EmpDAOImpl(Connection conn){ 10 this.conn = conn ; 11 } 12 public boolean doCreate(Emp emp) throws Exception{ 13 boolean flag = false ; 14 String sql = "INSERT INTO emp(empno,ename,job,hiredate,sal) VALUES (?,?,?,?,?)" ; 15 this.pstmt = this.conn.prepareStatement(sql) ; 16 this.pstmt.setInt(1,emp.getEmpno()) ; 17 this.pstmt.setString(2,emp.getEname()) ; 18 this.pstmt.setString(3,emp.getJob()) ; 19 this.pstmt.setDate(4,new java.sql.Date(emp.getHiredate().getTime())) ; 20 this.pstmt.setFloat(5,emp.getSal()) ; 21 if(this.pstmt.executeUpdate() > 0){ 22 flag = true ; 23 } 24 this.pstmt.close() ; 25 return flag ; 26 } 27 public List<Emp> findAll(String keyWord) throws Exception{ 28 List<Emp> all = new ArrayList<Emp>() ; 29 String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE ename LIKE ? OR job LIKE ?" ; 30 this.pstmt = this.conn.prepareStatement(sql) ; 31 this.pstmt.setString(1,"%"+keyWord+"%") ; 32 this.pstmt.setString(2,"%"+keyWord+"%") ; 33 ResultSet rs = this.pstmt.executeQuery() ; 34 Emp emp = null ; 35 while(rs.next()){ 36 emp = new Emp() ; 37 emp.setEmpno(rs.getInt(1)) ; 38 emp.setEname(rs.getString(2)) ; 39 emp.setJob(rs.getString(3)) ; 40 emp.setHiredate(rs.getDate(4)) ; 41 emp.setSal(rs.getFloat(5)) ; 42 all.add(emp) ; 43 } 44 this.pstmt.close() ; 45 return all ; 46 } 47 public Emp findById(int empno) throws Exception{ 48 Emp emp = null ; 49 String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE empno=?" ; 50 this.pstmt = this.conn.prepareStatement(sql) ; 51 this.pstmt.setInt(1,empno) ; 52 ResultSet rs = this.pstmt.executeQuery() ; 53 if(rs.next()){ 54 emp = new Emp() ; 55 emp.setEmpno(rs.getInt(1)) ; 56 emp.setEname(rs.getString(2)) ; 57 emp.setJob(rs.getString(3)) ; 58 emp.setHiredate(rs.getDate(4)) ; 59 emp.setSal(rs.getFloat(5)) ; 60 } 61 this.pstmt.close() ; 62 return emp ; 63 } 64 }
EmpDAOProxy.java
1 package cn.ftl.mysql; 2 3 import java.util.* ; 4 import java.sql.* ; 5 6 public class EmpDAOProxy implements IEmpDAO { 7 private DatabaseConnection dbc = null ; 8 private IEmpDAO dao = null ; 9 public EmpDAOProxy() throws Exception { 10 this.dbc = new DatabaseConnection() ; 11 this.dao = new EmpDAOImpl(this.dbc.getConnection()) ; 12 } 13 public boolean doCreate(Emp emp) throws Exception{ 14 boolean flag = false ; 15 try{ 16 if(this.dao.findById(emp.getEmpno()) == null){ 17 flag = this.dao.doCreate(emp) ; 18 } 19 }catch(Exception e){ 20 throw e ; 21 }finally{ 22 this.dbc.close() ; 23 } 24 return flag ; 25 } 26 public List<Emp> findAll(String keyWord) throws Exception{ 27 List<Emp> all = null ; 28 try{ 29 all = this.dao.findAll(keyWord) ; 30 }catch(Exception e){ 31 throw e ; 32 }finally{ 33 this.dbc.close() ; 34 } 35 return all ; 36 } 37 public Emp findById(int empno) throws Exception{ 38 Emp emp = null ; 39 try{ 40 emp = this.dao.findById(empno) ; 41 }catch(Exception e){ 42 throw e ; 43 }finally{ 44 this.dbc.close() ; 45 } 46 return emp ; 47 } 48 }
IEmpDAO.java
1 package cn.ftl.mysql; 2 import java.util.* ; 3 public interface IEmpDAO { 4 public boolean doCreate(Emp emp) throws Exception ; 5 public List<Emp> findAll(String keyWord) throws Exception ; 6 public Emp findById(int empno) throws Exception ; 7 }
TestDAOInsert.java
1 package cn.ftl.mysql; 2 public class TestDAOInsert{ 3 public static void main(String args[]) throws Exception{ 4 Emp emp = null ; 5 for(int x=0;x<5;x++){ 6 emp = new Emp() ; 7 emp.setEmpno(101 + x) ; 8 emp.setEname("FTL - " + x) ; 9 emp.setJob("JC - " + x) ; 10 emp.setHiredate(new java.util.Date()) ; 11 emp.setSal(5 * x) ; 12 DAOFactory.getIEmpDAOInstance().doCreate(emp) ; 13 } 14 } 15 }
TestDAOSelect.java
1 package cn.ftl.mysql; 2 import java.util.* ; 3 public class TestDAOSelect{ 4 public static void main(String args[]) throws Exception{ 5 List<Emp> all = DAOFactory.getIEmpDAOInstance().findAll("") ; 6 Iterator<Emp> iter = all.iterator() ; 7 while(iter.hasNext()){ 8 Emp emp = iter.next() ; 9 System.out.println(emp.getEmpno() + "、" + emp.getEname() + " --> " + emp.getJob()) ; 10 } 11 } 12 }
源码下载
【更多参考】
作者:小a玖拾柒
-------------------------------------------
个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~
本文版权归作者【小a玖拾柒】和【博客园】共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!