JDBC工具类

  1. package com.haier.bi.db;
  2. import java.sql.*;
  3. /**
  4. * JDBC工具类
  5. *
  6. * @author : initit@aliyun.com
  7. * @version 1.0.0
  8. * @date : 2021-09-13 22:13
  9. */
  10. public class JdbcUtil {
  11. /**
  12. * 获取数据库连接
  13. *
  14. * @param driver 驱动类
  15. * @param url 数据源地址
  16. * @param userName 用户名
  17. * @param password 密码
  18. * @return Connection
  19. */
  20. public static Connection getConnection(String driver, String url, String userName, String password) throws ClassNotFoundException, SQLException {
  21. //1.注册驱动
  22. Class.forName(driver);
  23. //2.获取数据库连接
  24. return DriverManager.getConnection(url, userName, password);
  25. }
  26. /**
  27. * 获取数据库预声明
  28. *
  29. * @param connection 数据库连接
  30. * @param sql SQL语句
  31. * @param params SQL参数
  32. * @return PreparedStatement
  33. */
  34. public static PreparedStatement getPreparedStatement(Connection connection, String sql, Object... params) throws SQLException {
  35. PreparedStatement preparedStatement = connection.prepareStatement(sql);
  36. if (null != params && params.length > 0) {
  37. for (int i = 1; i <= params.length; i++) {
  38. preparedStatement.setObject(i, params[i - 1]);
  39. }
  40. }
  41. return preparedStatement;
  42. }
  43. /**
  44. * 获取数据库结果集
  45. *
  46. * @param preparedStatement 数据库预声明
  47. * @return ResultSet
  48. */
  49. public static ResultSet getResultSet(PreparedStatement preparedStatement) throws SQLException {
  50. return preparedStatement.executeQuery();
  51. }
  52. /**
  53. * 关闭数据库连接并释放资源
  54. *
  55. * @param connection 连接
  56. * @param statement 声明
  57. * @param resultSet 结果集
  58. */
  59. public static void close(Connection connection, Statement statement, ResultSet resultSet) {
  60. //1.先关结果集
  61. closeResultSet(resultSet);
  62. //2.再关声明
  63. closeStatement(statement);
  64. //3.最后关连接
  65. closeConnection(connection);
  66. }
  67. /**
  68. * 关闭数据库连接
  69. *
  70. * @param connection 据库连接
  71. */
  72. public static void closeConnection(Connection connection) {
  73. if (connection != null) {
  74. try {
  75. connection.close();
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. }
  81. /**
  82. * 关闭数据库声明
  83. *
  84. * @param statement 数据库声明
  85. */
  86. public static void closeStatement(Statement statement) {
  87. if (statement != null) {
  88. try {
  89. statement.close();
  90. } catch (SQLException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. }
  95. /**
  96. * 关闭数据库结果集
  97. *
  98. * @param resultSet 数据库结果集
  99. */
  100. public static void closeResultSet(ResultSet resultSet) {
  101. if (resultSet != null) {
  102. try {
  103. resultSet.close();
  104. } catch (SQLException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }
  109. }

DB工具类

  1. package com.haier.bi.db;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.haier.bi.db.ds.Hive;
  4. import com.haier.bi.db.ds.MySQL;
  5. import com.haier.bi.exception.BizException;
  6. import java.sql.*;
  7. import java.util.LinkedHashMap;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. /**
  11. * 数据库操作类
  12. *
  13. * @author : initit@aliyun.com
  14. * @version 1.0.0
  15. * @date : 2021-09-13 22:13
  16. */
  17. public class Db {
  18. private final String driver;
  19. private final String url;
  20. private final String userName;
  21. private final String password;
  22. private Db(String driver, String url, String userName, String password) {
  23. this.driver = driver;
  24. this.url = url;
  25. this.userName = userName;
  26. this.password = password;
  27. }
  28. public static Db create(String driver, String url, String userName, String password) {
  29. return new Db(driver, url, userName, password);
  30. }
  31. public static Db createHive() {
  32. return create(Hive.driver, Hive.url, Hive.userName, Hive.password);
  33. }
  34. public static Db createMysql() {
  35. return create(MySQL.driver, MySQL.url, MySQL.userName, MySQL.password);
  36. }
  37. /**
  38. * 执行查询操作
  39. *
  40. * @param sql SQL语句
  41. * @param params SQL参数
  42. * @return List<LinkedHashMap < String, Object>>
  43. */
  44. public synchronized List<LinkedHashMap<String, Object>> query(String sql, Object... params) {
  45. List<LinkedHashMap<String, Object>> list = new LinkedList<>();
  46. Connection connection = null;
  47. PreparedStatement preparedStatement = null;
  48. ResultSet resultSet = null;
  49. try {
  50. connection = JdbcUtil.getConnection(driver, url, userName, password);
  51. preparedStatement = JdbcUtil.getPreparedStatement(connection, sql, params);
  52. resultSet = JdbcUtil.getResultSet(preparedStatement);
  53. ResultSetMetaData metaData = resultSet.getMetaData();
  54. int columnCount = metaData.getColumnCount();
  55. while (resultSet.next()) {
  56. LinkedHashMap<String, Object> map = new LinkedHashMap<>();
  57. for (int i = 1; i <= columnCount; i++) {
  58. String columnName = metaData.getColumnName(i);
  59. Object columnValue = resultSet.getObject(columnName);
  60. if (null == columnValue) {
  61. columnValue = "";
  62. }
  63. map.put(columnName, columnValue);
  64. }
  65. list.add(map);
  66. }
  67. return list;
  68. } catch (SQLException | ClassNotFoundException e) {
  69. String msg = StrUtil.format("执行查询操作异常,\nSQL:{}\nParams:{}", sql, params);
  70. throw new BizException(msg, e);
  71. } finally {
  72. JdbcUtil.close(connection, preparedStatement, resultSet);
  73. }
  74. }
  75. /**
  76. * 执行更新操作
  77. *
  78. * @param sql SQL语句
  79. * @param params SQL参数
  80. * @return 受影响的行数
  81. */
  82. public synchronized int update(String sql, Object... params) {
  83. Connection connection = null;
  84. PreparedStatement preparedStatement = null;
  85. try {
  86. connection = JdbcUtil.getConnection(driver, url, userName, password);
  87. preparedStatement = JdbcUtil.getPreparedStatement(connection, sql, params);
  88. return preparedStatement.executeUpdate();
  89. } catch (SQLException | ClassNotFoundException e) {
  90. String msg = StrUtil.format("执行更新操作异常,\nSQL:{}\nParams:{}", sql, params);
  91. throw new BizException(msg, e);
  92. } finally {
  93. JdbcUtil.close(connection, preparedStatement, null);
  94. }
  95. }
  96. }