import java.sql.*;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.DataSources;
public final class UsePoolBackedDataSource
{
public static void main(String[] argv)
{
try
{
/* 创建库和测试数据
create database x;
use x;
create table y (z int);
insert y (z) values (1);
insert y (z) values (2);
insert y (z) values (3);
* */
Class.forName("com.mysql.jdbc.Driver");
DataSource unpooled =
DataSources.unpooledDataSource("jdbc:mysql://localhost:3306/x",
"root","3");
//用户名root,密码是2,你的密码如果不是2,请填写你的密码,如果是空,请修改一下
//语句如下: set password for root@localhost=password('3');
DataSource pooled = DataSources.pooledDataSource( unpooled );
// get hold of a Connection an do stuff, in the usual way
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
con = pooled.getConnection();
System.out.println("下面是输出:");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM y");
while (rs.next())
System.out.println( rs.getString(1) );
}
finally
{
attemptClose(rs);
attemptClose(stmt);
attemptClose(con);
}
}
catch (Exception e)
{ e.printStackTrace(); }
}
static void attemptClose(ResultSet o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
}
static void attemptClose(Statement o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
}
static void attemptClose(Connection o)
{
try
{ if (o != null) o.close();}
catch (Exception e)
{ e.printStackTrace();}
}
private UsePoolBackedDataSource()
{}
}
c3p0 mysql 例子
4星 · 超过85%的资源 需积分: 9 45 浏览量
2008-08-30
23:51:19
上传
评论
收藏 1.02MB RAR 举报
zhngling
- 粉丝: 11
- 资源: 6
- 1
- 2
前往页