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));
}
}
}