jdbc的学习笔记4
数据库连接池
数据库连接池的必要性
java调用drivermanger类时,每次想数据库建立连接时都会将connection加载到内存中,执行完成后再断开连接,这样的方式会导致,会消耗大量的内存和时间,数据库的连接资源没有得到好的利用,若出现很多人同时操作会导致服务器崩溃。对于每次使用完都需要断开,如果出现异常未能操作完成,回导致数据哭的内存泄漏,这样的方式不能控制连接的对象数。
数据库连接池技术:(个人理解为公共交通工具)
为数据库连接建立一个连接池,预先缓冲一部分连接,当需要时取出使用完放回。
可以控制资源的数量,解决资源浪费或者系统崩溃。
c3p0
使用c3p0连接池技术创建一个数据库连接池
@Test
public void test () throws Exception {
//获取c3p0连接池
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/jdbc_learn?rewriteBatchedStatements=true" );
cpds.setUser("root");
cpds.setPassword("111111");
cpds.setInitialPoolSize(10);//设置连接池中初始连接数
Connection connection = cpds.getConnection();
System.out.println(connection);
}
使用配置文件进行创建c3p0数据库连接池
<?xml version="1.0" encoding="ISO-8859-1"?>
<c3p0-config>
<!-- This app is massive! -->
<!--获取建立连接的四个基本信息-->
<named-config name="HelloC3P0">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="JdbcUrl">jdbc:mysql://localhost:3306/jdbc_learn</property>
<property name="User">root</property>
<property name="Password">111111</property>
<!--c3po,?????????????????????????-->
<!-- 进行数据库连接处-->
<!--当连接数不够时一次向数据库申请的数据-->
<property name="acquireIncrement">5</property>
<!--初始化连接数为initialpoolsize-->
<property name="initialPoolSize">10</property>
<!--最小连接数-->
<property name="minPoolSize">10</property>
<!--最大连接数-->
<property name="maxPoolSize">100</property>
<!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">50</property>
<!--维护的最多的statement的个数-->
<property name="maxStatementsPerConnection">5</property>
<!--每个连接可以舒勇的最多的连接个数-->
</named-config>
</c3p0-config>
代码实现创建c3p0数据库连接池
@Test
public void test1() throws Exception {
ComboPooledDataSource cpds =new ComboPooledDataSource("HelloC3P0");//为配置文件中的
Connection conn=cpds.getConnection() ;
System.out.println(conn);
}
DBCP
@Test
public void test() throws SQLException {
BasicDataSource source=new BasicDataSource();//创建dbcp的数据库连接池
source.setDriverClassName("com.mysql.jdbc.Driver" );
source.setUrl("jdbc:mysql://localhost:3306/jdbc_learn");
source.setUsername("root");
source.setPassword("111111");
Connection connection = source.getConnection();
}
使用配置文件dbcp.propertise
driverClassname=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc_learn
username=root
password=111111
实现连接池
@Test
public void test1() throws Exception {
Properties pro =new Properties();
InputStream is = new FileInputStream(new File("src/dbcp.properties"));
pro.load(is);
BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(pro);
Connection connection = dataSource.getConnection();
}
德鲁伊Druid
配置文件
url=jdbc:mysql://localhost:3306/jdbc_learn
username=root
password=111111
driverClassname=com.mysql.jdbc.Driver
实现代码
public class Druidtest {
@Test
public void test() throws Exception {
Properties properties=new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("src/druid.propertise");
properties.load(is);
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection=dataSource.getConnection();
System.out.println(connection);
}}
DButils
实现增删改操作
@Test
public void test() throws Exception {
QueryRunner queryRunner = new QueryRunner();
Connection conn = JdbcUtils.getConnection();
String sql ="insert into Customers(name,email,birth)values(?,?,?)";
queryRunner.update(conn,sql,"蔡徐坤","caixukong.com","1998-01-09");
}
实现查询操作
@Test//返回一条记录使用beanhandler
public void test2() throws Exception {
QueryRunner runner=new QueryRunner();
Connection connection = JdbcUtils.getConnection();
String sql="select id,name,email,birth from customers where=?";
BeanHandler<Customertest> handler=new BeanHandler<>(Customertest.class);
Customertest query = runner.query(connection, sql, handler, 21);
System.out.println(query);
}
@Test、、返回多条记录使用beanlisthandler
public void test3() throws Exception {
QueryRunner runner=new QueryRunner();
Connection connection = JdbcUtils.getConnection();
String sql="select id,name,email,birth from customers where<?";
BeanListHandler<Customertest> handler=new BeanListHandler<>(Customertest.class);
List<Customertest> query = runner.query(connection, sql, handler, 21);
query.forEach(System.out::println);
}
//返回特殊值使用scalarhandler
总结
完成jdbc至此完,略显粗糙
剑谱最终页,无爱即是神