java mysql连接池比较_java 数据库连接池和普通连接 对比

packagecom.adao.c3p0;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importcom.mysql.jdbc.jdbc2.optional.MysqlDataSource;public classConnectionDemo {public static void main(String[] args) throwsSQLException, ClassNotFoundException {

System.out.println("使用连接池................................");for (int i = 0; i < 20; i++) {long beginTime =System.currentTimeMillis();

Connection conn=ConnectionManager.getInstance().getConnection();try{

PreparedStatement pstmt= conn.prepareStatement("select * from T_TERMINAL");

ResultSet rs=pstmt.executeQuery();while(rs.next()) {//do nothing...

}

}catch(SQLException e) {

e.printStackTrace();

}finally{try{

conn.close();

}catch(SQLException e) {

e.printStackTrace();

}

}long endTime =System.currentTimeMillis();

System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime -beginTime));

}

Connection con= null;//创建一个数据库连接

System.out.println("不使用连接池................................");for (int i = 0; i < 20; i++) {long beginTime =System.currentTimeMillis();//oracle

Class.forName("oracle.jdbc.driver.OracleDriver");//加载Oracle驱动程序

String url = "jdbc:oracle:thin:@192.168.127.156:1521:ORCL";

String user= "coll_platform";

String password= "coll_platform";

con= DriverManager.getConnection(url, user, password);//获取连接//mysql

// MysqlDataSource mds = newMysqlDataSource();

// mds.setURL("jdbc:mysql://localhost:3306/zww");

// mds.setUser("root");

// mds.setPassword("123456");

// Connection conn=mds.getConnection();try{

PreparedStatement pstmt= con.prepareStatement("select * from T_TERMINAL");

ResultSet rs=pstmt.executeQuery();while(rs.next()) {//do nothing...

}

}catch(SQLException e) {

e.printStackTrace();

}finally{try{

con.close();

}catch(SQLException e) {

e.printStackTrace();

}

}long endTime =System.currentTimeMillis();

System.out.println("第" + (i + 1) + "次执行花费时间为:"

+ (endTime -beginTime));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值