JDBCTools.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.xt.dsp.jdbc;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Date;
  9. import com.alibaba.fastjson.JSONObject;
  10. public class JDBCTools {
  11. private Connection conn;
  12. private Statement statement;
  13. private PreparedStatement ps;
  14. private ResultSet rs;
  15. private String driver = null;
  16. private String url = null;
  17. private String username = null;
  18. private String password = null;
  19. public JDBCTools(JSONObject connJson) {
  20. this.driver = connJson.getString("driver");
  21. this.url = connJson.getString("url");
  22. this.username = connJson.getString("username");
  23. this.password = connJson.getString("password");
  24. }
  25. public Connection getConnection() throws ClassNotFoundException, SQLException {
  26. Class.forName(this.driver);
  27. return conn = DriverManager.getConnection(url, username, password);
  28. }
  29. public void update(String sql, String condition) {
  30. try {
  31. conn = getConnection();
  32. ps = (PreparedStatement) conn.prepareStatement(sql);
  33. ps.setString(1, condition);
  34. ps.executeUpdate(sql);
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. } catch (ClassNotFoundException e) {
  38. e.printStackTrace();
  39. } finally {
  40. releaseDB(conn, statement);
  41. }
  42. }
  43. public void update1(String sql) {
  44. try {
  45. statement = (Statement) getConnection().createStatement();
  46. statement.executeUpdate(sql);
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. } catch (ClassNotFoundException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. } finally {
  53. releaseDB(conn, statement);
  54. }
  55. }
  56. public void testUpdete1() {
  57. String sql1 = "DELETE FROM Users WHERE userName='RORO'";
  58. update1(sql1);
  59. }
  60. public void releaseDB(Connection conn, Statement statement) {
  61. if (statement != null) {
  62. try {
  63. statement.close();
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. if (conn != null) {
  69. try {
  70. conn.close();
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }
  76. public void releaseDB(Connection conn, PreparedStatement ps) {
  77. if (ps != null) {
  78. try {
  79. ps.close();
  80. } catch (SQLException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. if (conn != null) {
  85. try {
  86. conn.close();
  87. } catch (SQLException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. }
  92. public void releaseDB() {
  93. if (ps != null) {
  94. try {
  95. ps.close();
  96. } catch (Exception e) {
  97. }
  98. }
  99. if (rs != null) {
  100. try {
  101. rs.close();
  102. } catch (Exception e) {
  103. }
  104. }
  105. if (conn != null) {
  106. try {
  107. conn.close();
  108. } catch (Exception e) {
  109. }
  110. }
  111. }
  112. public ResultSet getResultSet(String sql) {
  113. try {
  114. conn = getConnection();
  115. statement = (Statement) conn.createStatement();
  116. rs = statement.executeQuery(sql);
  117. } catch (SQLException e) {
  118. e.printStackTrace();
  119. } catch (ClassNotFoundException e) {
  120. e.printStackTrace();
  121. }
  122. return rs;
  123. }
  124. public ResultSet getPreResultSet(String sql, String condition) {
  125. try {
  126. conn = getConnection();
  127. ps = (PreparedStatement) conn.prepareStatement(sql);
  128. ps.setString(1, condition);
  129. rs = ps.executeQuery();
  130. } catch (SQLException e) {
  131. e.printStackTrace();
  132. } catch (ClassNotFoundException e) {
  133. e.printStackTrace();
  134. }
  135. return rs;
  136. }
  137. }