mysql怎么使用连接池


mysql使用连接池的示例:

1.手动配置连接池。

/**

*手动设置连接池

*/

publicvoiddemo1(){

//获得连接:

Connectionconn=null;

PreparedStatementpstmt=null;

ResultSetrs=null;

try{

//创建连接池:

ComboPooledDataSourcedataSource=newComboPooledDataSource();

//设置连接池的参数:

dataSource.setDriverClass("com.mysql.jdbc.Driver");

dataSource.setJdbcUrl("jdbc:mysql:///jdbctest");

dataSource.setUser("root");

dataSource.setPassword("abc");

dataSource.setMaxPoolSize(20);

dataSource.setInitialPoolSize(3);

//获得连接:

conn=dataSource.getConnection();

//编写Sql:

Stringsql="select*fromuser";

//预编译SQL:

pstmt=conn.prepareStatement(sql);

//设置参数

//执行SQL:

rs=pstmt.executeQuery();

while(rs.next()){

System.out.println(rs.getInt("uid")+""+rs.getString("username")+""+rs.getString("password")+""+rs.getString("name"));

}

}catch(Exceptione){

e.printStackTrace();

}finally{

JDBCUtils.release(rs,pstmt,conn);

}

}

2.使用配置文件配置连接池,配置文件xml如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<c3p0-config>

<default-config>

<propertyname="driverClass">com.mysql.jdbc.Driver</property>

<propertyname="jdbcUrl">jdbc:mysql:///jdbctest</property>

<propertyname="user">root</property>

<propertyname="password">abc</property>

<propertyname="initialPoolSize">5</property>

<propertyname="maxPoolSize">20</property>

</default-config>

</c3p0-config>

3.使用配置文件。

/**

*使用配置文件的方式

*/

publicvoiddemo2(){

Connectionconn=null;

PreparedStatementpstmt=null;

ResultSetrs=null;

try{

/*//获得连接:

ComboPooledDataSourcedataSource=newComboPooledDataSource();*/

//获得连接:

//conn=dataSource.getConnection();

conn=JDBCUtils2.getConnection();

//编写Sql:

Stringsql="select*fromuser";

//预编译SQL:

pstmt=conn.prepareStatement(sql);

//设置参数

//执行SQL:

rs=pstmt.executeQuery();

while(rs.next()){

System.out.println(rs.getInt("uid")+""+rs.getString("username")+""+rs.getString("password")+""+rs.getString("name"));

}

}catch(Exceptione){

e.printStackTrace();

}finally{

JDBCUtils2.release(rs,pstmt,conn);

}

}


上一篇:mysql怎么查询表格列属性

下一篇:mysql怎么插入时间属性


mysql数据库 msql 连接池
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器