自己封装的JDBC类

た 入场券 2021-09-07 22:30 487阅读 0赞

对JDBC进行一些简单的封装,可以方便在项目中调用,同时也减少了重复代码量,降低代码的冗余度,提高代码的可读性和美观。

#

  1. package JdbcUtil;
  2. import java.sql.*;
  3. /**
  4. * 简述:
  5. *数据库管理类
  6. * @author:LiYansheng
  7. * @date:2021/04/26 19:31
  8. * @version:
  9. */
  10. public class ConnectionManager {
  11. /**
  12. * 连接数据库的四大必需属性
  13. */
  14. private static final String driver = "com.mysql.cj.jdbc.Driver";
  15. private static final String url = "jdbc:mysql://localhost:3306/book_management?useSSL=false&serverTimezone=Asia/Shanghai";
  16. private static final String user = "root";
  17. private static final String psd = "root";
  18. /**
  19. * 静态块加载驱动
  20. */
  21. static {
  22. try {
  23. Class.forName(driver);
  24. System.out.println("加载驱动成功!");
  25. } catch (ClassNotFoundException e) {
  26. e.printStackTrace();
  27. System.out.println("加载驱动失败!");
  28. }
  29. }
  30. /**
  31. * 返回一个连接对象
  32. * @return
  33. */
  34. public static Connection getConnection() {
  35. Connection connection = null;
  36. try {
  37. connection = DriverManager.getConnection(url, user, psd);
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. return connection;
  42. }
  43. /**
  44. * 通用查询方法,返回结果集
  45. * @param sql
  46. * @param objects
  47. * @return
  48. * @throws SQLException
  49. */
  50. public static ResultSet query(String sql, Object[] objects) throws SQLException {
  51. Connection conn = ConnectionManager.getConnection();
  52. PreparedStatement pst = conn.prepareStatement(sql);
  53. ResultSet resultSet=null;
  54. // 判断Object是否为空,为空直接执行sql语句
  55. if (objects == null) {
  56. resultSet = pst.executeQuery();
  57. } else {
  58. for (int i = 0; i < objects.length; i++) {
  59. pst.setObject(i+1,objects[i]);
  60. }
  61. resultSet = pst.executeQuery();
  62. }
  63. return resultSet;
  64. }
  65. /**
  66. * 通用增删改方法
  67. * @param sql
  68. * @param objects
  69. * @return
  70. * @throws SQLException
  71. */
  72. public static int Update(String sql,Object[] objects) throws SQLException {
  73. Connection conn = ConnectionManager.getConnection();
  74. PreparedStatement pst = conn.prepareStatement(sql);
  75. // 判断数组是否为空
  76. try {
  77. if (objects == null || objects.equals("")) {
  78. return pst.executeUpdate();
  79. } else {
  80. for (int i = 0; i < objects.length; i++) {
  81. pst.setObject(i + 1, objects[i]);
  82. }
  83. return pst.executeUpdate();
  84. }
  85. } finally {
  86. closeall(conn, pst);
  87. }
  88. }
  89. /**
  90. * 返回结果集的二维数组形式,这个在JavaGUI里创建了表格要调用显示数据库的数据时可以用到
  91. * @param set
  92. * @return
  93. * @throws SQLException
  94. */
  95. public static Object[][] getSetArrays(ResultSet set) throws SQLException {
  96. Object[][] objects;
  97. set.last();
  98. int rowcount = set.getRow();
  99. ResultSetMetaData rsm = set.getMetaData();
  100. int colcount = rsm.getColumnCount();//获取列数
  101. // 创建二维数组
  102. objects = new Object[rowcount][colcount+1];
  103. set.first();
  104. for (int i = 0; i < rowcount; i++) {
  105. objects[i][0]=i+1;//给每一行的第一列添加序号
  106. for (int j = 1; j < colcount+1; j++) {
  107. objects[i][j] = set.getObject(j);
  108. }
  109. set.next();
  110. }
  111. return objects;
  112. }
  113. /**
  114. * 关闭资源
  115. * @param resultSet
  116. * @param statement
  117. * @param connection
  118. */
  119. public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) {
  120. try {
  121. if (resultSet != null) {
  122. resultSet.close();
  123. }
  124. } catch (SQLException e) {
  125. e.printStackTrace();
  126. }
  127. try {
  128. if (statement != null) {
  129. statement.close();
  130. }
  131. } catch (SQLException e) {
  132. e.printStackTrace();
  133. }
  134. try {
  135. if (connection != null && (!connection.isClosed())) {
  136. connection.close();
  137. }
  138. } catch (SQLException e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. public static void closeAll(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
  143. try {
  144. if (resultSet != null) {
  145. resultSet.close();
  146. }
  147. } catch (SQLException e) {
  148. e.printStackTrace();
  149. }
  150. try {
  151. if (preparedStatement != null) {
  152. preparedStatement.close();
  153. }
  154. } catch (SQLException e) {
  155. e.printStackTrace();
  156. }
  157. try {
  158. if (connection != null && (!connection.isClosed())) {
  159. connection.close();
  160. }
  161. } catch (SQLException e) {
  162. e.printStackTrace();
  163. }
  164. }
  165. public static void closeall(Connection c, PreparedStatement p) throws SQLException {
  166. c.close();
  167. p.close();
  168. }
  169. }

记录 一下。。。

发表评论

表情:
评论列表 (有 0 条评论,487人围观)

还没有评论,来说两句吧...

相关阅读

    相关 自己JDBC

    > 对JDBC进行一些简单的封装,可以方便在项目中调用,同时也减少了重复代码量,降低代码的冗余度,提高代码的可读性和美观。 package JdbcUtil