package datasource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* 演示c3p0的使用
*/
public class C3p0_ {
@Test
public void test() throws IOException, PropertyVetoException, SQLException {
//1.创建一个数据源对象
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
//2.通过配置文件获取相关连接
Properties properties = new Properties();
properties.load(new FileInputStream("src\\re.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
//给数据源comboDataSource 设置相关的参数
//下面连接的管理就是有comboDataSource 来管理
comboPooledDataSource.setDriverClass(driver);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(password);
//设置的连接数 set connection number
comboPooledDataSource.setInitialPoolSize(10);//ten connection is created
comboPooledDataSource.setMaxPoolSize(50);//max size is 50;
Connection connection = comboPooledDataSource.getConnection();
System.out.println("connection is completed");
connection.close();
}
//second :create connection by using model
//1.we should paste c3po.config.xml which is provided for c3p0 to src contents
//2.改文件定义了相关的参数。
@Test
public void testc3p002() throws SQLException {
ComboPooledDataSource hello = new ComboPooledDataSource("hello");
hello.getConnection();
System.out.println("ok");
hello.close();
}
}
package datasource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.util.Properties;
public class testDruid {
//1加入Druid jar 包
//2.加入配置文件 druid.properties,将文件拷贝到src目录
//3.创建properties文件
public void testDruid() throws Exception {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\druid.properties"));
//创建一个指定参数的数据库连接池,就像当于是自己的连接池。
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
System.out.println("连接成功");
connection.close();
}
}
jdbc.Utils
package com.wgs.jdbc.Utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.*;
/**
*完成mysql的连接和关闭资源。
*/
public class JDBCUtils {
private static String user;
private static String password;
private static String url;
private static String driver;
//在static代码块中实现初试化
static{
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\re.properties"));
} catch (IOException e) {
e.printStackTrace();
}
user = properties.getProperty("users");
password = properties.getProperty("password");
url = properties.getProperty("url");
driver = properties.getProperty("driver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//连接数据库
public static Connection getConnection(){
try {
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
//关闭相关的资源,
public static void close (ResultSet set, PreparedStatement statement, Connection connection){
//关闭结果集
try {
if (set != null){
set.close();
}
if (statement != null){
statement.close();
}
if (connection !=null){
connection.close();
}
} catch (SQLException throwables) {
throw new RuntimeException(throwables);
}
}
}