package study.datasoure;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* C3P0数据库连接池
* 步骤:
* 1、导入jar包 (c3p0-0.9.5.2.jar) 和 改该jar包的依赖包(mchange-commons-java-0.2.12.jar)
* 2、定义配置文件
* 配置名称:c3p0.properties 或者 c3p0-config.xml
*
* 3、创建核心对象 数据库连接池对象 ComboPooledDataSouce
* 获取连接:getConnection()
*
* 4、使用获取到的连接对象操作数据库
*/
public class C3p0 {
public static void main(String[] args) throws SQLException {
//1、创建数据库连接池对象
//DataSource ds = new ComboPooledDataSource("otherc3p0");//可以使用指定的配置
DataSource ds = new ComboPooledDataSource();
//2、获取连接对象
Connection conn = ds.getConnection();
//3、使用
//定义查询语句 使用?占位符
String select = "select * from emp where id = ?";
//获取执行SQL对象
PreparedStatement pstm = conn.prepareStatement(select);
//占位符参数
pstm.setInt(1,1002);
//执行查询语句
ResultSet res = pstm.executeQuery();
while (res.next()){
System.out.println(res.getInt("id"));
System.out.println(res.getString("ename"));
System.out.println(res.getInt("job_id"));
System.out.println(res.getInt("mgr"));
System.out.println(res.getDate("joindate"));
System.out.println(res.getDouble("salary"));
System.out.println(res.getDouble("bonus"));
System.out.println(res.getInt("dept_id"));
}
System.out.println(conn);
}
}
555