JobPlus知识库 IT DBA 文章
java之对Oracle当中的emp表实现增删改查

项目结构:

实现数据库连接:

[java]

  1. package com.soft.db;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.SQLException;  
  6. /* 
  7.  * 进行Oracle数据库对象的连接操作 
  8.  */  
  9. public class DBConnection {  
  10.     private String username="scott";  
  11.     private String password="admin";  
  12.     private String drive="oracle.jdbc.OracleDriver";  
  13.     private String url="jdbc:oracle:thin:@localhost:1521:orcl";  
  14.     private Connection connection;  
  15.     public DBConnection(){  
  16.         // TODO Auto-generated constructor stub  
  17.         try {  
  18.             Class.forName(drive);//根据drive类的地址来反向的对驱动类进行加载操作  
  19.             connection=DriverManager.getConnection(url, username, password);  
  20.         } catch (ClassNotFoundException | SQLException e) {  
  21.             System.out.println("数据库连接失败");  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25.     public Connection getConnection()  
  26.     {  
  27.         return this.connection;  
  28.     }  
  29.     public void closeConnection()  
  30.     {  
  31.         try {  
  32.             connection.close();  
  33.         } catch (SQLException e) {  
  34.             System.out.println("数据库连接关闭失败");  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38.     public static void main(String[] args) throws Exception{  
  39.         DBConnection dbConnection=new DBConnection();  
  40.         Connection connection=dbConnection.getConnection();  
  41.         if(connection!=null)  
  42.         {  
  43.             System.out.println("数据库连接成功");  
  44.         }  
  45.     }  
  46. }  

定义员工对象:

[java] 

  1. package com.soft.entity;  
  2.   
  3. import java.math.BigDecimal;  
  4. import java.sql.Date;  
  5.   
  6. public class EmpEnitity {  
  7.     private Integer empno;  
  8.     private String ename;  
  9.     private String  job;  
  10.     private Integer mgr;  
  11.     private Date hiredate;  
  12.     private BigDecimal sal;  
  13.     private BigDecimal comm;  
  14.     private Integer deptno;  
  15.     public EmpEnitity() {  
  16.         // TODO Auto-generated constructor stub  
  17.     }  
  18.     public EmpEnitity(Integer empno, String ename, String job, Integer mgr,  
  19.             Date hiredate, BigDecimal sal, BigDecimal comm, Integer deptno)   
  20.     {  
  21.         super();  
  22.         this.empno = empno;  
  23.         this.ename = ename;  
  24.         this.job = job;  
  25.         this.mgr = mgr;  
  26.         this.hiredate = hiredate;  
  27.         this.sal = sal;  
  28.         this.comm = comm;  
  29.         this.deptno = deptno;  
  30.     }  
  31.     /** 
  32.      * @return the empno 
  33.      */  
  34.     public Integer getEmpno() {  
  35.         return empno;  
  36.     }  
  37.     /** 
  38.      * @param empno the empno to set 
  39.      */  
  40.     public void setEmpno(Integer empno) {  
  41.         this.empno = empno;  
  42.     }  
  43.     /** 
  44.      * @return the ename 
  45.      */  
  46.     public String getEname() {  
  47.         return ename;  
  48.     }  
  49.     /** 
  50.      * @param ename the ename to set 
  51.      */  
  52.     public void setEname(String ename) {  
  53.         this.ename = ename;  
  54.     }  
  55.     /** 
  56.      * @return the job 
  57.      */  
  58.     public String getJob() {  
  59.         return job;  
  60.     }  
  61.     /** 
  62.      * @param job the job to set 
  63.      */  
  64.     public void setJob(String job) {  
  65.         this.job = job;  
  66.     }  
  67.     /** 
  68.      * @return the mgr 
  69.      */  
  70.     public Integer getMgr() {  
  71.         return mgr;  
  72.     }  
  73.     /** 
  74.      * @param mgr the mgr to set 
  75.      */  
  76.     public void setMgr(Integer mgr) {  
  77.         this.mgr = mgr;  
  78.     }  
  79.     /** 
  80.      * @return the hiredate 
  81.      */  
  82.     public Date getHiredate() {  
  83.         return hiredate;  
  84.     }  
  85.     /** 
  86.      * @param hiredate the hiredate to set 
  87.      */  
  88.     public void setHiredate(Date hiredate) {  
  89.         this.hiredate = hiredate;  
  90.     }  
  91.     /** 
  92.      * @return the sal 
  93.      */  
  94.     public BigDecimal getSal() {  
  95.         return sal;  
  96.     }  
  97.     /** 
  98.      * @param sal the sal to set 
  99.      */  
  100.     public void setSal(BigDecimal sal) {  
  101.         this.sal = sal;  
  102.     }  
  103.     /** 
  104.      * @return the comm 
  105.      */  
  106.     public BigDecimal getComm() {  
  107.         return comm;  
  108.     }  
  109.     /** 
  110.      * @param comm the comm to set 
  111.      */  
  112.     public void setComm(BigDecimal comm) {  
  113.         this.comm = comm;  
  114.     }  
  115.     /** 
  116.      * @return the deptno 
  117.      */  
  118.     public Integer getDeptno() {  
  119.         return deptno;  
  120.     }  
  121.     /** 
  122.      * @param deptno the deptno to set 
  123.      */  
  124.     public void setDeptno(Integer deptno) {  
  125.         this.deptno = deptno;  
  126.     }  
  127.     /* (non-Javadoc) 
  128.      * @see java.lang.Object#toString() 
  129.      */  
  130.     @Override  
  131.     public String toString() {  
  132.         return "EmpEnitity [empno=" + empno + ", ename=" + ename + ", job="  
  133.                 + job + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal="  
  134.                 + sal + ", comm=" + comm + ", deptno=" + deptno + "]";  
  135.     }  
  136.       
  137. }  

Dao接口定义

[java] 

  1. package com.soft.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.soft.entity.EmpEnitity;  
  6.   
  7. public interface EmpDao {  
  8.     public boolean saveEmp(EmpEnitity empEnitity) throws Exception;  
  9.     public boolean deleteEmp(Integer empno) throws Exception;  
  10.     public boolean updateEmp(EmpEnitity empEnitity) throws Exception;  
  11.     public List<EmpEnitity> queryAllEmp() throws Exception;  
  12.     public EmpEnitity queryEmpById(Integer empno) throws Exception;  
  13. //  对指定部门编号当中的所有员工对象进行查询操作  
  14.     public List<EmpEnitity> queryEmpByDeptno(Integer deptno) throws Exception;  

[java] 

  1.         public int deleteAllEmp(Integer[] items) throws Exception;  

[java] 

  1. }  

对增删改查接口的实现

[java] 

  1. package com.soft.daoImpl;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import com.soft.dao.EmpDao;  
  10. import com.soft.db.DBConnection;  
  11. import com.soft.entity.EmpEnitity;  
  12.   
  13. public class EmpDaoImpl implements EmpDao{  
  14.   
  15.     /* (non-Javadoc) 
  16.      * @see com.soft.dao.EmpDao#saveEmp(com.soft.entity.EmpEnitity) 
  17.      * 将指定的员工对象插入到数据库当中 
  18.      */  
  19.     @Override  
  20.     public boolean saveEmp(EmpEnitity empEnitity) throws Exception {  
  21.         DBConnection dbConnection=new DBConnection();  
  22.         Connection connection=dbConnection.getConnection();  
  23.         String sql;  
  24.             sql="insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,?,?,?,?)";  
  25.             PreparedStatement preparedStatement=connection.prepareStatement(sql);  
  26.             preparedStatement.setInt(1,empEnitity.getEmpno());  
  27.             preparedStatement.setString(2, empEnitity.getEname());  
  28.             preparedStatement.setString(3, empEnitity.getJob());  
  29.             preparedStatement.setInt(4, empEnitity.getMgr());  
  30.             preparedStatement.setDate(5, empEnitity.getHiredate());  
  31.             preparedStatement.setBigDecimal(6, empEnitity.getSal());  
  32.             preparedStatement.setBigDecimal(7, empEnitity.getComm());  
  33.             preparedStatement.setInt(8, empEnitity.getDeptno());  
  34.         ResultSet resultSet=preparedStatement.executeQuery();  
  35.         dbConnection.closeConnection();  
  36.         return false;  
  37.     }  
  38.   
  39.     /* (non-Javadoc) 
  40.      * @see com.soft.dao.EmpDao#deleteEmp(java.lang.Integer) 
  41.      * 根据职工编号来对指定的职工对象当中的数据进行删除操作 
  42.      */  
  43.     @Override  
  44.     public boolean deleteEmp(Integer empno) throws Exception {  
  45.         DBConnection dbConnection=new DBConnection();  
  46.         Connection connection=dbConnection.getConnection();  
  47.         String sql=null;  
  48.             sql="delete from emp where emp.empno=?";  
  49.             PreparedStatement preparedStatement=connection.prepareStatement(sql);  
  50.             preparedStatement.setInt(1,empno);  
  51.             boolean flag=preparedStatement.execute();  
  52.         dbConnection.closeConnection();  
  53.         return flag;  
  54.     }  

[java] 

  1.     /** 
  2.      * 通过员工ID来对多个员工对象进行删除操作 
  3.      * @throws Exception  
  4.      * 其中整形常量的值分别为:   
  5.         SUCCESS_NO_INFO -2      执行批处理操作成功但是该操作受影响的行数是未知的   
  6.         EXECUTE_FAILED -3 
  7.      */  
  8.     @Override  
  9.     public int deleteAllEmp(Integer[] items) throws Exception{  
  10.         DBConnection dbConnection=new DBConnection();  
  11.         Connection connection=dbConnection.getConnection();  
  12.         int[] s=new int[0];  
  13.         try {  
  14.             connection.setAutoCommit(false);//取消Connection连接对象当中的自动提交事务的设置  
  15.             String sql=null;  
  16.                 sql="delete from emp where emp.empno=?";  
  17.                 PreparedStatement preparedStatement=connection.prepareStatement(sql);  
  18.                 for (Integer integer : items) {  
  19.                     preparedStatement.setInt(1,integer.intValue());  
  20. //              将当前要进行执行的SQL语句添加到批处理对象当中  
  21.                     preparedStatement.addBatch();  
  22.                 }  
  23. //          进行批处理的执行操作  
  24.                 s=preparedStatement.executeBatch();  
  25.                 System.out.println("s="+s[0]);  
  26. //          进行事务的提交  
  27.                 connection.commit();  
  28.         } catch (Exception e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31. //          进行事务的回滚操作  
  32.             connection.rollback();  
  33.         }  
  34.         return s.length;  
  35.     }  

[java] 

  1.         /* (non-Javadoc)  

[java]

  1.  * @see com.soft.dao.EmpDao#updateEmp(com.soft.entity.EmpEnitity)  
  2.  */  
  3. @Override  
  4. public boolean updateEmp(EmpEnitity empEnitity) throws Exception {  
  5.     DBConnection dbConnection=new DBConnection();  
  6.     Connection connection=dbConnection.getConnection();  
  7.     String sql=null;  
  8.         sql="update emp set ename=?,job=?,mgr=?,hiredate=?,sal=?,comm=?,deptno=? where empno=?";  
  9.         PreparedStatement preparedStatement=connection.prepareStatement(sql);  
  10.         preparedStatement.setString(1, empEnitity.getEname());  
  11.         preparedStatement.setString(2, empEnitity.getJob());  
  12.         preparedStatement.setInt(3, empEnitity.getMgr());  
  13.         preparedStatement.setDate(4, empEnitity.getHiredate());  
  14.         preparedStatement.setBigDecimal(5, empEnitity.getSal());  
  15.         preparedStatement.setBigDecimal(6, empEnitity.getComm());  
  16.         preparedStatement.setInt(7, empEnitity.getDeptno());  
  17.         preparedStatement.setInt(8,empEnitity.getEmpno());  
  18.     boolean flag=preparedStatement.execute();  
  19.     dbConnection.closeConnection();  
  20.     return flag;  
  21. }  
  22.   
  23. /* (non-Javadoc) 
  24.  * @see com.soft.dao.EmpDao#queryAllEmp() 
  25.  */  
  26. @Override  
  27. public List<EmpEnitity> queryAllEmp() throws Exception {  
  28.     List<EmpEnitity> list=new ArrayList<>();  
  29.     EmpEnitity empEnitity;  
  30.     DBConnection dbConnection=new DBConnection();  
  31.     Connection connection=dbConnection.getConnection();  
  32.     String sql=null;  
  33.         sql="select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp";  
  34.         PreparedStatement preparedStatement=connection.prepareStatement(sql);  
  35.         ResultSet resultSet=preparedStatement.executeQuery();  
  36.         while(resultSet.next())  
  37.         {  
  38.             empEnitity=new EmpEnitity();  
  39.             empEnitity.setEmpno(resultSet.getInt(1));  
  40.             empEnitity.setEname(resultSet.getString(2));  
  41.             empEnitity.setJob(resultSet.getString(3));  
  42.             empEnitity.setMgr(resultSet.getInt(4));  
  43.             empEnitity.setHiredate(resultSet.getDate(5));  
  44.             empEnitity.setSal(resultSet.getBigDecimal(6));  
  45.             empEnitity.setComm(resultSet.getBigDecimal(7));  
  46.             empEnitity.setDeptno(resultSet.getInt(8));  
  47.             list.add(empEnitity);  
  48.         }  
  49.     dbConnection.closeConnection();  
  50.     return list;  
  51. }  
  52.   
  53. /* (non-Javadoc) 
  54.  * @see com.soft.dao.EmpDao#queryEmpById(java.lang.Integer) 
  55.  */  
  56. @Override  
  57. public EmpEnitity queryEmpById(Integer empno) throws Exception {  
  58.     DBConnection dbConnection=new DBConnection();  
  59.     Connection connection=dbConnection.getConnection();  
  60.     EmpEnitity empEnitity = null;  
  61.     String sql=null;  
  62.         sql="select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where emp.empno=?";  
  63.         PreparedStatement preparedStatement=connection.prepareStatement(sql);  
  64.         preparedStatement.setInt(1, empno);  
  65.         ResultSet resultSet=preparedStatement.executeQuery();  
  66.             if(resultSet.next())  
  67.             {  
  68.                 empEnitity=new EmpEnitity();  
  69.                 empEnitity.setEmpno(resultSet.getInt(1));  
  70.                 empEnitity.setEname(resultSet.getString(2));  
  71.                 empEnitity.setJob(resultSet.getString(3));  
  72.                 empEnitity.setMgr(resultSet.getInt(4));  
  73.                 empEnitity.setHiredate(resultSet.getDate(5));  
  74.                 empEnitity.setSal(resultSet.getBigDecimal(6));  
  75.                 empEnitity.setComm(resultSet.getBigDecimal(7));  
  76.                 empEnitity.setDeptno(resultSet.getInt(8));  
  77.             }  
  78.     dbConnection.closeConnection();  
  79.     return empEnitity;  
  80. }  
  81.   
  82. /* (non-Javadoc) 
  83.  * @see com.soft.dao.EmpDao#queryEmpByDeptno(java.lang.Integer) 
  84.  * 根据部门编号来查询指定部门当中的所有的员工信息 
  85.  */  
  86. @Override  
  87. public List<EmpEnitity> queryEmpByDeptno(Integer deptno) throws Exception {  
  88.     DBConnection dbConnection=new DBConnection();  
  89.     Connection connection=dbConnection.getConnection();  
  90.     List<EmpEnitity> list=new ArrayList<>();  
  91.     EmpEnitity empEnitity;  
  92.     String sql=null;  
  93.         sql="select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp where emp.deptno=?";  
  94.         PreparedStatement preparedStatement=connection.prepareStatement(sql);  
  95.         preparedStatement.setInt(1, deptno);  
  96.         ResultSet resultSet=preparedStatement.executeQuery();  
  97.         while(resultSet.next())  
  98.         {  
  99.             empEnitity=new EmpEnitity();  
  100.             empEnitity.setEmpno(resultSet.getInt(1));  
  101.             empEnitity.setEname(resultSet.getString(2));  
  102.             empEnitity.setJob(resultSet.getString(3));  
  103.             empEnitity.setMgr(resultSet.getInt(4));  
  104.             empEnitity.setHiredate(resultSet.getDate(5));  
  105.             empEnitity.setSal(resultSet.getBigDecimal(6));  
  106.             empEnitity.setComm(resultSet.getBigDecimal(7));  
  107.             empEnitity.setDeptno(resultSet.getInt(8));  
  108.             list.add(empEnitity);  
  109.         }  
  110.     dbConnection.closeConnection();  
  111.     return list;  
  112. }  

测试用例类

[java]

  1. package com.soft.test;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import java.math.BigDecimal;  
  6. import java.sql.Date;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import org.junit.After;  
  11. import org.junit.Before;  
  12. import org.junit.Test;  
  13.   
  14. import com.soft.daoImpl.EmpDaoImpl;  
  15. import com.soft.entity.EmpEnitity;  
  16.   
  17. import junit.framework.TestCase;  
  18. /** 
  19.  *  
  20.  * @author Administrator 
  21.  *进行用例测试的时候,当要执行某一个单独的测试方法的时候,双击方法名然后进行Junit的执行即可 
  22.  */  
  23. public class EmpDaoImplTest{  
  24.     EmpDaoImpl empDaoImpl;  
  25.     @Before  
  26.     public void init()  
  27.     {  
  28.         System.out.println("进行初始化操作");  
  29.         empDaoImpl=new EmpDaoImpl();  
  30.     }  
  31.     @Test  
  32.     public void saveEmpTest()  
  33.     {  
  34.         EmpEnitity empEntity;  
  35.         System.out.println("对saveEmp方法进行测试");  
  36.         boolean flag=false;  
  37.         empEntity=new EmpEnitity  
  38.                 (  
  39.                 7943,"小清","ANALYST", 7698,Date.valueOf("2018-06-23") ,new BigDecimal(7894.05),new BigDecimal(1200),20  
  40.                 );  
  41.         try   
  42.         {  
  43.             flag=empDaoImpl.saveEmp(empEntity);  
  44.         } catch (Exception e)  
  45.         {  
  46.             System.out.println("职工信息添加失败");  
  47.             e.printStackTrace();  
  48.         }  
  49.         assertTrue("员工信息存放成功", flag==true);  
  50.     }  
  51.       
  52.     @Test  
  53.     public void deleteEmpTest()  
  54.     {  
  55.         System.out.println("对deleteEmp方法进行测试");  
  56.         boolean flag=false;  
  57.         try {  
  58.             flag=empDaoImpl.deleteEmp(7943);  
  59.         } catch (Exception e) {  
  60.             System.out.println("对指定员工对象当中的信息进行删除失败");  
  61.             e.printStackTrace();  
  62.         }  
  63.     }  
  64.     @Test  
  65.     public void queryAllEmpTest()  
  66.     {  
  67.         System.out.println("对queryAllEmp方法进行测试");  
  68.         List<EmpEnitity> empEnitities=new ArrayList<>();  
  69.         try {  
  70.             empEnitities=empDaoImpl.queryAllEmp();  
  71.         } catch (Exception e) {  
  72.             System.out.println("对全体员工对象当中的信息进行查询失败");  
  73.             e.printStackTrace();  
  74.         }  
  75.         for (EmpEnitity empEnitity : empEnitities) {  
  76.             System.out.println(empEnitity);  
  77.         }  
  78.     }  
  79.     @Test  
  80.     public void queryEmpByIdTest()  
  81.     {  
  82.         EmpEnitity empEnitity=null;  
  83.         System.out.println("对queryEmpById方法进行测试");  
  84.         try {  
  85.             empEnitity=empDaoImpl.queryEmpById(7499);  
  86.         } catch (Exception e) {  
  87.             System.out.println("根据员工编号来对员工信息进行查询失败");  
  88.             e.printStackTrace();  
  89.         }  
  90.         System.out.println(empEnitity);  
  91.     }  
  92.     @Test  
  93.     public void queryEmpByDeptnoTest()  
  94.     {  
  95.         System.out.println("对queryEmpByDeptno方法进行测试");  
  96.         List<EmpEnitity> empEnitities=new ArrayList<>();  
  97.         try {  
  98.             empEnitities=empDaoImpl.queryEmpByDeptno(20);  
  99.         } catch (Exception e) {  
  100.             System.out.println("对指定部门当中的全体员工对象当中的信息进行查询失败");  
  101.             e.printStackTrace();  
  102.         }  
  103.         for (EmpEnitity empEnitity : empEnitities) {  
  104.             System.out.println(empEnitity);  
  105.         }  
  106.     }  
  107.     /** 
  108.      * 在对某一对象进行修改之前必须要先查询到该对象 
  109.      */  
  110.     @Test  
  111.     public void updateEmpTest()  
  112.     {  
  113.         try {  
  114.             EmpEnitity empEnitity=empDaoImpl.queryEmpById(1008);  
  115.             empEnitity.setEname("周小清");  
  116.             boolean flag=empDaoImpl.updateEmp(empEnitity);  
  117.         } catch (Exception e) {  
  118.             System.out.println("对指定编号的职工对象进行修改失败");  
  119.             e.printStackTrace();  
  120.         }  
  121.     }  
  122.     @After  
  123.     public void finash()  
  124.     {  
  125.         System.out.println("用例测试结束");  
  126.     }  

[html] 

      @Test  

  1.     public void deleteAllTest()  
  2.     {  
  3.         System.out.println("对deleteAllTest方法进行测试");  
  4.         Integer items[]={new Integer(7943),new Integer(7935)};  
  5.         try {  
  6.             empDaoImpl.deleteAllEmp(items);  
  7.         } catch (Exception e) {  
  8.             System.out.println("根据学号对多个职工对象进行删除操作失败");  
  9.             e.printStackTrace();  
  10.         }  
  11.     }  


}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
50人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序