DbConnection.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.skyversation.poiaddr.config;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Service;
  4. import javax.annotation.PostConstruct;
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. @Service
  11. public class DbConnection {
  12. private static DbConnection dbConnection;
  13. public Connection connection;
  14. public static DbConnection getInstance() {
  15. if (dbConnection == null) {
  16. dbConnection = new DbConnection();
  17. }
  18. return dbConnection;
  19. }
  20. private Statement stmt;
  21. @Value("${app.db.username}")
  22. private String db_user_name;
  23. @Value("${app.db.password}")
  24. private String db_password;
  25. @Value("${app.db.driver}")
  26. private String jdbcDriver;
  27. @Value("${app.db.jdbc_url}")
  28. private String jdbcURL;
  29. @PostConstruct
  30. public void initDbConnection() throws SQLException {
  31. // 加载驱动
  32. System.out.println("加载驱动!");
  33. dbConnection = this;
  34. try {
  35. Class.forName(jdbcDriver);
  36. } catch (Exception e) {
  37. System.err.println("加载驱动异常:" + e);
  38. System.exit(1);
  39. }
  40. System.out.println("配置文件中的数据库用户名:" + db_user_name + ";密码:" + db_password);
  41. connection = DriverManager.getConnection(jdbcURL, db_user_name, db_password);
  42. stmt = connection.createStatement();
  43. }
  44. // 插入、删除、更新
  45. public int updateSql(String sqlStr) throws SQLException {
  46. try {
  47. return stmt.executeUpdate(sqlStr);
  48. } catch (Exception e) {
  49. if (connection == null || connection.isClosed()) {
  50. System.out.println("数据库异常断开,尝试重新连接!");
  51. connection = DriverManager.getConnection(jdbcURL, db_user_name, db_password);
  52. stmt = connection.createStatement();
  53. return this.updateSql(sqlStr);
  54. } else {
  55. System.err.println(e.getMessage());
  56. return -1;
  57. }
  58. }
  59. }
  60. // 只查询
  61. public List<Map<String, Object>> runSqlStr(String sqlStr) throws SQLException {
  62. try {
  63. ResultSet rs = stmt.executeQuery(sqlStr);
  64. ResultSetMetaData rsmd = rs.getMetaData();
  65. int size = rsmd.getColumnCount();
  66. List<Map<String, Object>> requestData = new ArrayList<>();
  67. while (rs.next()) {
  68. Map<String, Object> dataItem = new HashMap<>();
  69. for (int i = 0; i < size; i++) {
  70. dataItem.put(rsmd.getColumnName(i + 1), rs.getString(i + 1));
  71. }
  72. requestData.add(dataItem);
  73. }
  74. rs.close();
  75. return requestData;
  76. } catch (Exception e) {
  77. if (connection == null || connection.isClosed()) {
  78. System.out.println("数据库异常断开,尝试重新连接!");
  79. connection = DriverManager.getConnection(jdbcURL, db_user_name, db_password);
  80. stmt = connection.createStatement();
  81. return this.runSqlStr(sqlStr);
  82. } else {
  83. System.err.println(e.getMessage());
  84. return null;
  85. }
  86. }
  87. }
  88. }