Java JDBC数据源

mac2022-06-30  83

 

数据源有2种:

普通数据源     即数据库驱动自带的数据源连接池     包括数据库驱动自带的连接池,以及DBCP、C3P0等常用的第三方连接池。

 

 

数据库驱动自带的数据源

1 //从properties文件加载数据源配置 2 Properties properties = new Properties(); 3 InputStream is = Class.forName("test.Test").getResourceAsStream("/mysqlDataSource.properties"); 4 properties.load(is); 5 String url=properties.getProperty("url"); 6 String user = properties.getProperty("user"); 7 String password = properties.getProperty("password"); 8 9 //设置普通数据源,因为使用的是MysqlDataSource类,驱动自然只能是mysql的驱动,这个类已经把数据库驱动设置好了,不用我们设置。 10 MysqlDataSource mysqlDataSource = new MysqlDataSource(); 11 mysqlDataSource.setUrl(url); //还有个方法setURL(),这2个方法完全一样,因为setURL()的函数体就是调用setUrl() 12 mysqlDataSource.setUser(user); 13 mysqlDataSource.setPassword(password); 14 15 //获取连接,操作数据库 16 Connection connection = mysqlDataSource.getConnection(); 17 String sql = "insert into student_tb (name,age,score) values (?,?,?)"; 18 PreparedStatement preparedStatement = connection.prepareStatement(sql); 19 preparedStatement.setString(1,"chy"); 20 preparedStatement.setInt(2,20); 21 preparedStatement.setInt(3,100); 22 preparedStatement.executeUpdate(); 23 preparedStatement.close(); 24 connection.close();

 

我导入的是Mysql的数据库驱动,mysql驱动提供的自然是mysql数据源,已经默认注册了mysql数据库驱动,我们不必手动注册。如果导入的其他数据库的驱动,提供的自然就是该种数据库的数据源。

 

一般来说,数据库驱动都会提供2种数据源:普通数据源、连接池。

mysql驱动提供的普通数据源是MysqlDataSource,连接池是MysqlConnectionPoolDataSource,看名字就知道是连接池数据源。

 

 

 

 

数据库驱动自带的连接池

1 //从properties文件加载数据源配置 2 Properties properties = new Properties(); 3 InputStream is = Class.forName("test.Test").getResourceAsStream("/mysqlDataSource.properties"); 4 properties.load(is); 5 String url=properties.getProperty("url"); 6 String user = properties.getProperty("user"); 7 String password = properties.getProperty("password"); 8 9 //设置连接池 10 MysqlConnectionPoolDataSource poolDataSource = new MysqlConnectionPoolDataSource(); 11 poolDataSource.setURL(url); 12 poolDataSource.setUser(user); 13 poolDataSource.setPassword(password); 14 15 //获取连接,操作数据库 16 Connection connection = poolDataSource.getConnection(); 17 String sql = "insert into student_tb (name,age,score) values (?,?,?)"; 18 PreparedStatement ps = connection.prepareStatement(sql); 19 ps.setString(1,"chy"); 20 ps.setInt(2,20); 21 ps.setInt(3,99); 22 ps.executeUpdate(); 23 connection.close();

 

普通数据源采用直连数据库的方式,调用  getConnection() 获取连接时,实际上底层仍是调用  DriverManager.getConnection(url, user, password); 来获取连接;关闭连接时,就是实实在在地关闭连接,释放资源。

连接池是在服务器上创建一个连接池,预先建立一些数据库连接,放在连接池中,调用  getConnection() 获取连接时,只是从连接池中取出一个连接,调用close()关闭连接时,并不是真的关闭连接,是由连接池回收连接,下次还可以接着用。创建数据库连接是很花时间的,使用连接池减少了时间开销。

 

数据源适合需要多次创建数据库连接的应用,如果不使用数据源,一个一个地DriverManager.getConnection(url, user, password);传入url、user、password创建连接,很麻烦。

连接池数据源适合需要创建多个连接的应用。

 

 

 

 

DBCP数据源

DBCP是Apache的一个开源项目,Tomcat的连接池就是使用DBCP来实现的。

 

使用DBCP需要2个jar包:

commons-dbcp.jar   dbcp的核心包,下载压缩包后里面有commons-pool.jar   dbcp的依赖,需要单独去下载

这2个都是在apache上下载,下载的时候要注意对应我们的JDK版本(实际上是对应JDK中JDBC的版本)。

 

此外,还需要2个额外的jar包:

commons-logging.jar   日志包数据库驱动

 

DBCP常用的数据源类是BasicDataSource。

1 //从properties文件加载数据源配置 2 Properties properties = new Properties(); 3 InputStream is = Class.forName("test.Test").getResourceAsStream("/dataSource.properties"); 4 properties.load(is); 5 String driverStr = properties.getProperty("driver"); 6 String url=properties.getProperty("url"); 7 String username = properties.getProperty("username"); 8 String password = properties.getProperty("password"); 9 10 //设置连接池 11 BasicDataSource dataSource = new BasicDataSource(); 12 //这些第三方的数据源不知道我们使用的是何种数据库,所以要手动设置数据库驱动 13 dataSource.setDriverClassName(driverStr); //注意是setDriverClassName(),参数才是String 14 /* 15 也可以这样写: 16 java.sql.Driver driver = DriverManager.getDriver(driverStr); 17 dataSource.setDriver(driver); //参数是java.sql.Driver 18 Driver类在多个包下都有,注意不要写错了。 19 建议使用前一种,更简单,不容易出错。 20 */ 21 dataSource.setUrl(url); 22 dataSource.setUsername(username); //注意是setUsername(),不是setUser() 23 dataSource.setPassword(password); 24 25 //从数据源获取连接获取连接,操作数据库 26 Connection connection = dataSource.getConnection(); 27 String sql = "insert into student_tb (name,age,score) values (?,?,?)"; 28 PreparedStatement ps = connection.prepareStatement(sql); 29 ps.setString(1,"chy"); 30 ps.setInt(2,20); 31 ps.setInt(3,99); 32 ps.executeUpdate(); 33 connection.close();

 

可以通过工厂来创建数据源:

BasicDataSource dataSource= BasicDataSourceFactory.createDataSource(properties); //参数是Properties类型

十分简便,但对properties中的key有严格要求,比如说:

setUsername()  =>  去掉set,后面部分变为camel写法,username。

如果key写错了,比如写成了user,会报错: Access denied for user ''@'localhost' (using password: YES)  ,有时候报的是NO。

 

可以给数据源设置一些其他参数,比如:

  dataSource.setInitialSize(5); //设置初始连接数,初始化连接池时会创建5个连接,放到连接池中 dataSource.setMinIdle(2); //设置最小空闲连接数,连接池中至少有2个连接是空闲的。idle,闲置的、无所事事。 dataSource.setMaxTotal(20); //设置最大连接数,连接池中最多可以有20个连接 //......

 

 

 

 

使用properties文件直接配置DBCP数据源

driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/my_db?serverTimezone=GMT username=chy password=abcd minIdle=2 maxIdle=10 initialSize=15 maxTotal=30

 

1 //从properties文件加载数据源配置 2 Properties properties = new Properties(); 3 InputStream is = Class.forName("test.Test").getResourceAsStream("/dataSource.properties"); 4 properties.load(is); 5 6 //设置连接池 7 BasicDataSource dataSource= BasicDataSourceFactory.createDataSource(properties); 8 9 //从数据源获取连接获取连接,操作数据库 10 Connection connection = dataSource.getConnection(); 11 String sql = "insert into student_tb (name,age,score) values (?,?,?)"; 12 PreparedStatement ps = connection.prepareStatement(sql); 13 ps.setString(1,"chy"); 14 ps.setInt(2,20); 15 ps.setInt(3,99); 16 ps.executeUpdate(); 17 connection.close();

 

数据库驱动自带的数据源、c3p0直接使用properties文件配置,很容易出错,不推荐。

dbcp可以直接使用properties文件配置,不容易出错。

 

 

 

 

C3P0数据源

C3P0是mchange的开源项目,相比于DBCP,C3P0有自动回收闲置连接的功能,性能更优。

下载,解压后lib文件夹下有3个jar包,把c3p0.jar、mchange-commons-java.jar这2个jar包添加到项目中,带oracle的那个是Oracel才用的。

把数据库驱动添加到项目中。

DBCP是Apache的,要添加自家的日志包commons-logging.jar,C3P0则不必添加日志包。

 

C3P0常用的数据源类是ComboPooledDataSource。

1 //从properties文件加载数据源配置 2 Properties properties = new Properties(); 3 InputStream is = Class.forName("test.Test2").getResourceAsStream("/dataSource.properties"); 4 properties.load(is); 5 String driver = properties.getProperty("driver"); 6 String jdbcUrl=properties.getProperty("jdbcUrl"); 7 String user = properties.getProperty("user"); 8 String password = properties.getProperty("password"); 9 10 //配置数据源 11 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 12 dataSource.setDriverClass(driver); //只有这个方法,没有setDriver() 13 dataSource.setJdbcUrl(jdbcUrl); //注意是setJdbcUrl(),和其他数据源不同 14 dataSource.setUser(user); 15 dataSource.setPassword(password); 16 17 //从数据源获取连接,操作数据库 18 Connection connection = dataSource.getConnection(); 19 String sql = "insert into student_tb (name,age,score) values (?,?,?)"; 20 PreparedStatement ps = connection.prepareStatement(sql); 21 ps.setString(1,"chy"); 22 ps.setInt(2,20); 23 ps.setInt(3,99); 24 ps.executeUpdate(); 25 connection.close();

 

相比于DBCP,C3P0可以设置更多的配置参数,比如:

  dataSource.setMaxStatements(10); dataSource.setMaxStatementsPerConnection(10);

 这些配置一般不写死在程序中,而是写在配置文件中,从配置文件中加载配置。

 

 

DBCP不具备自动回收空闲连接的功能,C3P0具备。

 

转载于:https://www.cnblogs.com/chy18883701161/p/11374731.html

最新回复(0)