数据库连接池的简介与使用
1、为什么有了普通的数据库连接还要学连接池?
对于普通的数据库连接来说,我们每次去创建连接,使用完后关闭,这样的话,对于我们的资源造成了极大的浪费;而且,对于高并发以及高吞吐量的情况产生时,数据库连接会达到一种供不应求的境地。所以这个时候我们就需要用一个叫做连接池的东西。顾名思义,就相当于我们吃饭时用的筷子,不可能每次使用完就把筷子扔了,所以说,我们所存放的筷子的容器就相当于我们所提到的连接池。数据库连接池的运用能够极大地节省资源,并有效的解决了高并发和高吞吐量这种情况的发生。
2、数据库连接池包括什么?
(1)、导包(c3p0-0.9.1.2.jar)。
(2)、导入配置文件(不是必须的,但是在企业中一定使用配置文件),如果不使用配置文件,就要在代码中设置数据库的4个参数;项目分为开发环境和运行环境,在开发时,开发人员接触不到公司实际数据库,数据是机密。 但是在开发后也要更改数据库 ,但是不需要更改代码,那就需要使用配置文件。具体什么是配置文件下面会介绍。
(3)、导入自己写的工具类(初始化连接池,获得连接池的方法)
(4)、在dao中使用工具类获得连接池(有的工具类可以帮我们去创建连接)
QueryRunner queryRunner = new QueryRunner(C3p0Util.getDataSource());
这样的话:
Connection connection = MyUtils.getConnection();
和row=queryRunner.update(connection,sql,params);中的connection就都可以不用写了。
具体代码奉上:
C3P0工具类:
package util; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.SQLException; public class C3p0Util { private static ComboPooledDataSource dataSource = null; static { /*DataSource连接池,连接都在此处放置*/ dataSource = new ComboPooledDataSource("mysql"); } public static ComboPooledDataSource getDataSource(){ return dataSource; } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } }
查询操作代码(C3p0):
public static List<User>get(){ //Connection connection = MyUtils.getConnection(); Connection connection = null; try { connection = C3p0Util.getConnection(); } catch (SQLException e) { e.printStackTrace(); } //获取执行sql语句的对象 QueryRunner queryRunner = new QueryRunner(); String sql = "select * from user"; /*工具类提供的两种方法*/ //queryRunner.update(); //对数据库进行更改操作 //queryRunner.query(); //对数据库进行查询操作 List<User> list=null; try { /*将User对象传入进去,并将User的可执行文件反馈给该对象*/ list = queryRunner.query(connection, sql, new BeanListHandler<User>(User.class)); } catch (SQLException e) { e.printStackTrace(); } return list; }
配置文件
Java中的一些键值对的配置文件专门提供了这样的类和文件名
properties 文件后缀名
Properties 读取.properties文件的类
读取配置文件:
1、将配置文件变成流
2、用Java提供的Properties类去加载这个流
3、读取properties对象的getProperty的方法,这个方法需要一个参数,键名返回一个值(字符串型)
具体代码奉上:
database.properties代码:
package propertiesRead; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Demo { public static void main(String[] args) throws IOException { //Demo.class传入一个Demo类 getClassLoader()类的加载器 getResourceAsStream()以流的方式获取资源 InputStream in = Demo.class.getClassLoader().getResourceAsStream("database.properties"); //读取文件资源 Properties properties = new Properties(); properties.load(in); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String driverClass = properties.getProperty("driverClass"); String jdbcUrl = properties.getProperty("jdbcUrl"); System.out.println(user); System.out.println(password); System.out.println(driverClass); System.out.println(jdbcUrl); } }
c3p0-config.xml配置文件:
<?xml version="1.0" encoding="utf-8" ?> <c3p0-config> <named-config name="mysql"> <property name="user">root</property> <property name="password">123</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/order</property> </named-config> </c3p0-config>
Demo.java代码:
package propertiesRead; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Demo { public static void main(String[] args) throws IOException { //Demo.class传入一个Demo类 getClassLoader()类的加载器 getResourceAsStream()以流的方式获取资源 InputStream in = Demo.class.getClassLoader().getResourceAsStream("database.properties"); //读取文件资源 Properties properties = new Properties(); properties.load(in); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String driverClass = properties.getProperty("driverClass"); String jdbcUrl = properties.getProperty("jdbcUrl"); System.out.println(user); System.out.println(password); System.out.println(driverClass); System.out.println(jdbcUrl); } }