ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring-连接数据-存手写

2021-12-17 12:03:50  阅读:173  来源: 互联网

标签:jdbc String getString spring connection dataSource rb 手写 连接


数据源的手动写:

纯手动的:

下面的这个是用的java写的,而需要的连接池: c3p0

  @Test
   public  void test1() throws Exception {
//
       ComboPooledDataSource dataSource = new ComboPooledDataSource();
       dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
       dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true");
//       dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/db1&serverTimezone=HongKong");
       dataSource.setUser("root");
       dataSource.setPassword("522636");

       Connection connection = dataSource.getConnection();
       System.out.println(connection);
       connection.close();
  }

下面的这个也是使用的Java书写的--》 使用的连接池是 druid ---》

@Test
   public  void test2() throws SQLException {
//         druid
       DruidDataSource dataSource = new DruidDataSource();
       dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
       dataSource.setUrl("jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true");
       dataSource.setUsername("root");
       dataSource.setPassword("522636");

       DruidPooledConnection connection =dataSource.getConnection();
       System.out.println(connection);
       connection.close();
  }

 

上面的这些方法:是可以的,但是呢,我们在写程序的时候还是需要低耦合的,所以这个时候,我们就会将里面对于数据库的连接对象的内容写到持久化的集合 properties里面去,后期上传以后,我们更改数据库的时候,只需要更改配置文件jdbc。properties就可以了。就实现了低耦合这样的·一个概念。

 

下面的就是使用配置文件以后的一种书写方法:

首页就是连接数据的配置文件: jdbc。properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db1?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=522636

c3p0 的:

 @Test
   public  void  test3() throws Exception {
//     du c3p0 de
       ResourceBundle rb = ResourceBundle.getBundle("jdbc");
       String driver = rb.getString("jdbc.driver");
       String url = rb.getString("jdbc.url");
       String username = rb.getString("jdbc.username");
       String password = rb.getString("jdbc.password");

//       chuang jian shu ju yuan dui x
       ComboPooledDataSource dataSource  = new ComboPooledDataSource();
       dataSource.setDriverClass(driver);
       dataSource.setJdbcUrl(url);
       dataSource.setUser(username);
       dataSource.setPassword(password);

       Connection connection = dataSource.getConnection();
       System.out.println(connection);
       connection.close();
  }

druid的

  @Test
   public  void  test4() throws SQLException {
//       druid de shuju lianjiedui
       ResourceBundle rb = ResourceBundle.getBundle("jdbc");
       String driver= rb.getString("jdbc.driver");
       String url= rb.getString("jdbc.url");
       String username=rb.getString("jdbc.username");
       String password= rb.getString("jdbc.password");

       DruidDataSource dataSource = new DruidDataSource();
       dataSource.setDriverClassName(driver);
       dataSource.setUrl(url);
       dataSource.setUsername(username);
       dataSource.setPassword(password);

       DruidPooledConnection connection = dataSource.getConnection();
       System.out.println(connection);
       connection.close();
  }

 

标签:jdbc,String,getString,spring,connection,dataSource,rb,手写,连接
来源: https://www.cnblogs.com/954321xx/p/15701717.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有