• 使用jdbc连接池同步数据


    配置文件

    1. driver=com.mysql.cj.jdbc.Driver
    2. url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
    3. username=root
    4. password=root

    工具类 

    1. import org.apache.commons.dbcp2.BasicDataSource;
    2. import org.springframework.core.io.ClassPathResource;
    3. import org.springframework.core.io.Resource;
    4. import java.sql.Connection;
    5. import java.sql.SQLException;
    6. import java.util.Properties;
    7. public class DataSourceConfig {
    8. private static BasicDataSource dataSource;
    9. private static ThreadLocal tl;
    10. static {
    11. try {
    12. tl = new ThreadLocal<>();
    13. Properties prop = new Properties();
    14. //读取配置文件
    15. Resource resource= new ClassPathResource("db.properties");
    16. prop.load(resource.getInputStream());
    17. //初始化数据库连接池
    18. dataSource = new BasicDataSource();
    19. dataSource.setDriverClassName(prop.getProperty("driver"));
    20. dataSource.setUrl(prop.getProperty("url"));
    21. dataSource.setUsername(prop.getProperty("username"));
    22. dataSource.setPassword(prop.getProperty("password"));
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. }
    26. }
    27. /**
    28. * 获取一个数据库连接
    29. *
    30. * @return
    31. * @throws ClassNotFoundException
    32. * @throws SQLException
    33. */
    34. public static Connection getConnection() throws ClassNotFoundException, SQLException {
    35. if(tl.get() == null){
    36. Connection conn = dataSource.getConnection();
    37. tl.set(conn);
    38. return conn;
    39. }else {
    40. return tl.get();
    41. }
    42. }
    43. /**
    44. * 关闭数据库连接
    45. *
    46. * @param conn
    47. */
    48. public static void closeConnection() {
    49. try {
    50. Connection conn = tl.get();
    51. if (conn != null) {
    52. conn.close();
    53. }
    54. tl.remove();
    55. } catch (Exception e) {
    56. e.printStackTrace();
    57. }
    58. }
    59. /**
    60. * 开启事务
    61. *
    62. * @throws SQLException
    63. */
    64. public static void TransBegin() throws SQLException {
    65. Connection conn = tl.get();
    66. conn.setAutoCommit(false);
    67. }
    68. /**
    69. * 关闭事务
    70. *
    71. * @throws SQLException
    72. */
    73. public static void TransEnd() throws SQLException {
    74. Connection conn = tl.get();
    75. conn.setAutoCommit(true);
    76. }
    77. /**
    78. * 提交事务
    79. *
    80. * @throws SQLException
    81. */
    82. public static void commit() throws SQLException {
    83. Connection conn = tl.get();
    84. conn.commit();
    85. }
    86. /**
    87. * 回滚事务
    88. *
    89. * @throws SQLException
    90. */
    91. public static void rollback() throws SQLException {
    92. Connection conn = tl.get();
    93. conn.rollback();
    94. }
    95. }

    使用示例 

    1. public interface DataSync {
    2. List> getResulst();
    3. }
    4. import java.sql.Connection;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8. //yml配置文件配置一个值,控制在存在环境属性且具有特定值的情况下才启用Bean注册
    9. @ConditionalOnProperty(name="datasync.value", havingValue="dev")
    10. @Component
    11. public class DataSyncImpl implements DataSync{
    12. public List> getResulst() {
    13. List> mapList = new ArrayList<>();
    14. try{
    15. //获得连接
    16. Connection conn = DataSourceConfig.getConnection();
    17. StringBuffer sb = new StringBuffer();
    18. String sql = "select code,name from 表名 ";
    19. sb.append(sql);
    20. sb.append(" GROUP BY code");
    21. //执行sql
    22. PreparedStatement ps = conn.prepareStatement(sb.toString());
    23. //获得结果
    24. ResultSet rs = ps.executeQuery();
    25. //遍历
    26. while (rs.next()){
    27. Map map = new HashMap<>();
    28. //取出查询的列值
    29. map.put("code",rs.getString(1));
    30. map.put("value",rs.getString(2));
    31. mapList.add(map);
    32. }
    33. ps.close();
    34. rs.close();
    35. } catch (Exception e) {
    36. e.printStackTrace();
    37. } finally{
    38. DataSourceConfig.closeConnection();
    39. }
    40. return mapList;
    41. }
    42. }

  • 相关阅读:
    【那些反爬和反反爬】JA3指纹
    KubeVela交付
    Mysql中自增主键是如何工作的
    eBPF(三)
    算法leetcode|84. 柱状图中最大的矩形(rust重拳出击)
    C++异常捕获
    干洗店洗鞋店线上下单小程序方便快捷
    QT页面布局方法大全
    用移位距离和标定神经网络迭代次数
    DeepMind CEO 最新《纽约时报》专访:AGI 将使能源变得廉价甚至免费,货币性质也将发生转变
  • 原文地址:https://blog.csdn.net/jah5201/article/details/128182894