6 MySQL数据库--数据库连接池( 五 )


2.3 开源连接池的使用 (1)C3p0
PS:jar包
提取码:1234
/*使用C3P0连接池1.导入jar包2.导入配置文件到src目录下3.创建c3p0连接池对象4.获取数据库连接进行使用*/public class C3P0Demo1 {public static void main(String[] args) throws Exception{//创建c3p0连接池对象DataSource dataSource = new ComboPooledDataSource();//获取数据库连接进行使用Connection con = dataSource.getConnection();//查询全部学生信息String sql = "SELECT * FROM student";PreparedStatement pst = con.prepareStatement(sql);ResultSet rs = pst.executeQuery();while(rs.next()) {System.out.println(rs.getInt("sid") + "\t" + rs.getString("name") + "\t" + rs.getInt("age") + "\t" + rs.getDate("birthday"));}//释放资源rs.close();pst.close();con.close();// 将连接对象归还池中}}
(2)Druid(德鲁伊)
过程:
1.导入jar包
提取码:1234
2.编写配置文件druid.(在链接里,下面我也再书写了一次),放在里面
3.通过集合加载配置文件
用一个File命名为:druid.
=com.mysql.jdbc.
url=jdbc:mysql://:3306/db15

6  MySQL数据库--数据库连接池

文章插图
=root
==*jo&
#初始连接数
=5
#最大连接数
=10
#最大等待时间(毫秒),超了之后就爆错了
=3000
public class DruidDemo1 {public static void main(String[] args)throws Exception {//通过Properties集合加载配置文件InputStream is = DruidDemo1.class.getClassLoader().getResourceAsStream("druid.properties");Properties prop = new Properties();prop.load(is);//通过Druid连接池工厂类获取数据库连接池对象DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);//获取数据库连接,进行使用Connection con = dataSource.getConnection();//查询全部学生信息String sql = "SELECT * FROM student";PreparedStatement pst = con.prepareStatement(sql);ResultSet rs = pst.executeQuery();while(rs.next()) {System.out.println(rs.getInt("sid") + "\t" + rs.getString("name") + "\t" + rs.getInt("age") + "\t" + rs.getDate("birthday"));}//释放资源rs.close();pst.close();con.close();// 将连接对象归还池中}}
2.4 数据库连接池工具类
/*数据库连接池工具类*/public class DataSourceUtils {//1.私有构造方法DataSourceUtils(){}//2.定义DataSource数据源变量private static DataSource dataSource;//3.提供静态代码块,完成配置文件的加载和获取连接池对象static {try{//加载配置文件InputStream is = DruidDemo1.class.getClassLoader().getResourceAsStream("druid.properties");Properties prop = new Properties();prop.load(is);//获取数据库连接池对象dataSource = DruidDataSourceFactory.createDataSource(prop);} catch(Exception e) {e.printStackTrace();}}//4.提供获取数据库连接的方法public static Connection getConnection() {Connection con = null;try {con = dataSource.getConnection();} catch (SQLException e) {e.printStackTrace();}return con;}//5.提供获取数据库连接池的方法public static DataSource getDataSource() {return dataSource;}//6.提供释放资源的方法public static void close(Connection con, Statement stat, ResultSet rs) {if(con != null) {try {con.close();} catch (SQLException e) {e.printStackTrace();}}if(stat != null) {try {stat.close();} catch (SQLException e) {e.printStackTrace();}}if(rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}}public static void close(Connection con, Statement stat) {close(con,stat,null);}}
public class DruidDemo2{public static void main(String[] args) throws Exception {Connection con =DataSourceUtils.getConnection();String sql ="SELECT * FROM student";PreparedStatement pst =con.prepareStatement(sql);ResultSet rs = pst.executeQuery();while(rs.next()){System.out.println(rs.getInt("sid")+"\t"+rs.getString("NAME")+"\t"+rs.getInt("age")+"\t"+rs.getDate("birthday"));}rs.close();pst.close();con.close();}}