JDBC连接Oracle数据库后实现对emp 表数据的增删改查(转载)

链接:https://blog.csdn.net/JAVA_START008/article/details/53073875?locationNum=3&fps=1

  1. 1、创建JDBC连接数据库类:  
[html]  view plain  copy
 
  1.   
[java]  view plain  copy
 
  1. public class DBConnection {    
  2.         privatestatic String url = "jdbc:oracle:thin:@localhost:1521:orcl";    
  3.         privatestatic String user = "scott";    
  4.         privatestatic String password = "tiger";    
  5.          static{    
  6.                   try{    
  7.                            //加载驱动类    
  8.                            Class.forName("oracle.jdbc.driver.OracleDriver");    
  9.                   }catch (ClassNotFoundException e) {    
  10.                            e.printStackTrace();    
  11.                   }    
  12.          }    
  13.         publicstatic Connection getConn(){    
  14.                   //获取连接对象    
  15.                   Connectioncon = null;    
  16.                   try{    
  17.                            con= DriverManager.getConnection(url, user, password);    
  18.                   }catch (SQLException e) {    
  19.                            e.printStackTrace();    
  20.                   }    
  21.                                     return con;    
  22.          }    
  23.                  public static voidclose(Connectionconn,PreparedStatement ps, ResultSet rs){    
  24.                   if(rs!= null){    
  25.                            try{    
  26.                                     rs.close();    
  27.                            }catch (SQLException e) {    
  28.                                     e.printStackTrace();    
  29.                            }    
  30.                   }    
  31.                   if(ps!= null){    
  32.                            try{    
  33.                                     ps.close();    
  34.                            }catch (SQLException e) {    
  35.                                     e.printStackTrace();    
  36.                            }    
  37.                   }    
  38.                   if(conn!= null){    
  39.                            try{    
  40.                                     conn.close();    
  41.                            }catch (SQLException e) {    
  42.                                     e.printStackTrace();    
  43.                            }    
  44.                   }    
  45.          }    
  46. }   
 

 
[html]  view plain  copy
 
  1. <span style="font-family:monospace;white-space:pre;background-color:rgb(240,240,240);">2、创建emp 表所对应的的实体类:</span>  
[html]  view plain  copy
 
  1.   
[java]  view plain  copy
 
  1. public class Emp {  
  2.    
  3.           private int empno;  
  4.           private String ename;  
  5.           private String job;  
  6.           private int mgr;  
  7.           private Date hiredate;  
  8.           private double sal;  
  9.           private double comm;  
  10.           private int deptno;  
  11.            
  12.           public Emp(){}  
  13.            
  14.           public Emp(int empno, String ename, String job, int mgr, Date hiredate,  
  15.                                double sal, double comm, int deptno) {  
  16.                     super();  
  17.                     this.empno = empno;  
  18.                     this.ename = ename;  
  19.                     this.job = job;  
  20.                     this.mgr = mgr;  
  21.                     this.hiredate = hiredate;  
  22.                     this.sal = sal;  
  23.                     this.comm = comm;  
  24.                     this.deptno = deptno;  
  25.           }  
  26.            
  27.           @Override  
  28.           public String toString() {  
  29.                     return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job  
  30.                                          + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal  
  31.                                          + ", comm=" + comm + ", deptno=" + deptno + "]";  
  32.           }  
  33.           public int getEmpno() {  
  34.                     return empno;  
  35.           }  
  36.           public void setEmpno(int empno) {  
  37.                     this.empno = empno;  
  38.           }  
  39.           public String getEname() {  
  40.                     return ename;  
  41.           }  
  42.           public void setEname(String ename) {  
  43.                     this.ename = ename;  
  44.           }  
  45.           public String getJob() {  
  46.                     return job;  
  47.           }  
  48.           public void setJob(String job) {  
  49.                     this.job = job;  
  50.           }  
  51.           public int getMgr() {  
  52.                     return mgr;  
  53.           }  
  54.           public void setMgr(int mgr) {  
  55.                     this.mgr = mgr;  
  56.           }  
  57.           public Date getHiredate() {  
  58.                     return hiredate;  
  59.           }  
  60.           public void setHiredate(Date hiredate) {  
  61.                     this.hiredate = hiredate;  
  62.           }  
  63.           public double getSal() {  
  64.                     return sal;  
  65.           }  
  66.           public void setSal(double sal) {  
  67.                     this.sal = sal;  
  68.           }  
  69.           public double getComm() {  
  70.                     return comm;  
  71.           }  
  72.           public void setComm(double comm) {  
  73.                     this.comm = comm;  
  74.           }  
  75.           public int getDeptno() {  
  76.                     return deptno;  
  77.           }  
  78.           public void setDeptno(int deptno) {  
  79.                     this.deptno = deptno;  
  80.           }  
  81.            
  82. }  
 
[html]  view plain  copy
 
  1.   
[html]  view plain  copy
 
  1. <span style="font-family:Arial, Helvetica, sans-serif;"</span><span style="font-family:Arial, Helvetica, sans-serif;">3、创建实现业务操作类,实现增删改查具体方法:</span>  
 
[html]  view plain  copy
 
  1.   

 

[java]  view plain  copy
 
  1. public class EmpDao {  
  2.    
  3.          /** 
  4.           * 增加一条数据 
  5.           * insert into emp values(....) 
  6.           */  
  7.          public boolean addOne(Emp emp){  
  8.                    boolean f = false;  
  9.                     
  10.                    //2.定义sql语句  
  11.                    String sql = "insert into emp values(?,?,?,?,?,?,?,?)";  
  12.                    //1.获取数据库连接对象  
  13.                    //3.获取preparedStatement对象,以发送sql语句到数据库  
  14.                    try (Connection conn = DBConnection.getConn();PreparedStatement ps = conn.prepareStatement(sql)){  
  15.                             //通过ps对象给sql语句设置参数?  
  16.                             ps.setInt(1, emp.getEmpno());  
  17.                             ps.setString(2, emp.getEname());  
  18.                             ps.setString(3, emp.getJob());  
  19.                             ps.setInt(4, emp.getMgr());  
  20.                             ps.setDate(5, emp.getHiredate());  
  21.                             ps.setDouble(6, emp.getSal());  
  22.                             ps.setDouble(7, emp.getComm());  
  23.                             ps.setInt(8, emp.getDeptno());  
  24.                             //4.执行sql语句  
  25.                             int row = ps.executeUpdate();  
  26.                             if(row != 0){  
  27.                                      f = true;  
  28.                             }  
  29.                    } catch (SQLException e) {  
  30.                             e.printStackTrace();  
  31.                    }  
  32.                    return f;  
  33.          }  
  34.           
  35.          /** 
  36.           * 根据ID删除 
  37.           * @param id 
  38.           * @return 
  39.           */  
  40.          public boolean delById(int id){  
  41.                    boolean f = false;  
  42.                    String sql = "delete from emp where empno=?";  
  43.                    try(Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(sql);) {  
  44.                             ps.setInt(1, id);  
  45.                             int row = ps.executeUpdate();  
  46.                             if(row > 0){  
  47.                                      f = true;  
  48.                             }  
  49.                    } catch (SQLException e) {  
  50.                             e.printStackTrace();  
  51.                    }  
  52.                    return f;  
  53.          }  
  54.           
  55.          /** 
  56.           * 根据id修改 
  57.           * @return 
  58.           */  
  59.          public boolean updateById(Emp emp){  
  60.                    boolean b = false;  
  61.                    String s = "update emp set ename=?,job=?,mgr=?,sal=?,comm=?,deptno=?,hiredate=? where empno=?";  
  62.                    try (Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(s);){  
  63.                             ps.setString(1, emp.getEname());  
  64.                             ps.setString(2, emp.getJob());  
  65.                             ps.setInt(3, emp.getMgr());  
  66.                             ps.setDouble(4, emp.getSal());  
  67.                             ps.setDouble(5, emp.getComm());  
  68.                             ps.setInt(6, emp.getDeptno());  
  69.                             ps.setDate(7, emp.getHiredate());  
  70.                             ps.setInt(8, emp.getEmpno());  
  71.                             int row = ps.executeUpdate();  
  72.                             if(row > 0){  
  73.                                      b = true;  
  74.                             }  
  75.                    } catch (SQLException e) {  
  76.                             e.printStackTrace();  
  77.                    }  
  78.                    return b;  
  79.          }  
  80.           
  81.          /** 
  82.           * 根据id查询 
  83.           * @param id 
  84.           * @return 
  85.           */  
  86.          public Emp queryById(int id){  
  87.                    Emp emp = null;  
  88.                    ResultSet rs = null;  
  89.                    String sql = "select * from emp where empno=?";  
  90.                    try (Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(sql);){  
  91.                             ps.setInt(1, id);  
  92.                             //执行  
  93.                             rs = ps.executeQuery();  
  94.                             //结果集光标向下移动一行,如果查询到数据,返回true,否则返回false  
  95.                             boolean b = rs.next();  
  96.                             if(b){  
  97.                                      //查询到了数据,取出数据  
  98.                                      int empno = rs.getInt("EMPNO");  
  99.                                      String ename = rs.getString(2);  
  100.                                      String job = rs.getString(3);  
  101.                                      int mgr = rs.getInt(4);  
  102.                                      Date hiredate = rs.getDate(5);  
  103.                                      double sal = rs.getDouble(6);  
  104.                                      double comm = rs.getDouble(7);  
  105.                                      int deptno = rs.getInt(8);  
  106.                                      //将数据封装成对象  
  107. //                                  emp = new Emp();  
  108. //                                  emp.setEmpno(empno);  
  109.                                      emp = new Emp(empno,ename,job,mgr,hiredate,sal,comm,deptno);  
  110.                             }  
  111.                    } catch (SQLException e) {  
  112.                             e.printStackTrace();  
  113.                    } finally{  
  114.                             try {  
  115.                                      rs.close();  
  116.                             } catch (SQLException e) {  
  117.                                      e.printStackTrace();  
  118.                             }  
  119.                    }  
  120.                    return emp;  
  121.          }  
  122.           
  123.          /** 
  124.           * 查询全部数据 
  125.           * @return 
  126.           */  
  127.          public List<Emp> queryAll(){  
  128.                    List<Emp> list = new ArrayList<>();  
  129.                    String sql = "select * from emp";  
  130.                    try (Connection con = DBConnection.getConn();PreparedStatement ps = con.prepareStatement(sql);ResultSet rs = ps.executeQuery();){  
  131.                             //只要rs光标所在当前行有数据,rs.next()就返回true  
  132.                             while(rs.next()){  
  133.                                      //取出当前行数据,封装成一个Emp对象,放入list集合  
  134.                                      int empno = rs.getInt("EMPNO");  
  135.                                      String ename = rs.getString(2);  
  136.                                      String job = rs.getString(3);  
  137.                                      int mgr = rs.getInt(4);  
  138.                                      Date hiredate = rs.getDate(5);  
  139.                                      double sal = rs.getDouble(6);  
  140.                                      double comm = rs.getDouble(7);  
  141.                                      int deptno = rs.getInt(8);  
  142.                                      list.add(new Emp(empno,ename,job,mgr,hiredate,sal,comm,deptno));  
  143.                             }  
  144.                    } catch (SQLException e) {  
  145.                             e.printStackTrace();  
  146.                    }  
  147.                    return list;  
  148.          }  
  149.           
  150.          /** 
  151.           * 调用无返回值的存储过程 
  152.           * @throws Exception 
  153.           */  
  154.          public void callPro110() throws Exception{  
  155.                    Connection conn = DBConnection.getConn();  
  156.                    CallableStatement cs = conn.prepareCall("{call pro110}");  
  157.                    //执行  
  158.                    cs.execute();  
  159.          }  
  160.           
  161. }  
 

猜你喜欢

转载自www.cnblogs.com/suguowen/p/9184449.html