1. 程式人生 > >JDBC連接Oracle數據庫後實現對emp 表數據的增刪改查(轉載)

JDBC連接Oracle數據庫後實現對emp 表數據的增刪改查(轉載)

設置 cstatic 數據庫連接 tno 集合 tro rep jdbc連接 turn

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

  1. 1、創建JDBC連接數據庫類:
[html] view plain copy
[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
[java] view plain copy
  1. public class Emp {
  2. private int empno;
  3. private String ename;
  4. private String job;
  5. private int mgr;
  6. private Date hiredate;
  7. private double sal;
  8. private double comm;
  9. private int deptno;
  10. public Emp(){}
  11. public Emp(int empno, String ename, String job, int mgr, Date hiredate,
  12. double sal, double comm, int deptno) {
  13. super();
  14. this.empno = empno;
  15. this.ename = ename;
  16. this.job = job;
  17. this.mgr = mgr;
  18. this.hiredate = hiredate;
  19. this.sal = sal;
  20. this.comm = comm;
  21. this.deptno = deptno;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job
  26. + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal
  27. + ", comm=" + comm + ", deptno=" + deptno + "]";
  28. }
  29. public int getEmpno() {
  30. return empno;
  31. }
  32. public void setEmpno(int empno) {
  33. this.empno = empno;
  34. }
  35. public String getEname() {
  36. return ename;
  37. }
  38. public void setEname(String ename) {
  39. this.ename = ename;
  40. }
  41. public String getJob() {
  42. return job;
  43. }
  44. public void setJob(String job) {
  45. this.job = job;
  46. }
  47. public int getMgr() {
  48. return mgr;
  49. }
  50. public void setMgr(int mgr) {
  51. this.mgr = mgr;
  52. }
  53. public Date getHiredate() {
  54. return hiredate;
  55. }
  56. public void setHiredate(Date hiredate) {
  57. this.hiredate = hiredate;
  58. }
  59. public double getSal() {
  60. return sal;
  61. }
  62. public void setSal(double sal) {
  63. this.sal = sal;
  64. }
  65. public double getComm() {
  66. return comm;
  67. }
  68. public void setComm(double comm) {
  69. this.comm = comm;
  70. }
  71. public int getDeptno() {
  72. return deptno;
  73. }
  74. public void setDeptno(int deptno) {
  75. this.deptno = deptno;
  76. }
  77. }




[html] view plain copy
 


[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
 


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

JDBC連接Oracle數據庫後實現對emp 表數據的增刪改查(轉載)