其他分享
首页 > 其他分享> > spring-连接数据-存手写

spring-连接数据-存手写

作者:互联网

数据源的手动写:

纯手动的:

下面的这个是用的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