測試用DBCP連接池和不用DBCP連接池對數(shù)據(jù)庫的訪問效率
package com.pccw.test.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestDBCPFunctionsByTimes implements Runnable{
private long dateTime1=0;
private static int count=0;
public static void main(String[] args) {
//System.out.println("main start");
TestDBCPFunctionsByTimes test=new TestDBCPFunctionsByTimes();
test.startup();
//System.out.println("main over");
}
public void startup(){
for (int i = 0; i <80; i++) {
Thread t=new Thread(this);
t.start();
}
}
@Override
public void run() {
if(dateTime1==0){ //當?shù)谝粋€線程進入時開始記錄時間
dateTime1=System.currentTimeMillis();
System.out.println("dateTime1 is "+dateTime1);
}
String sql="select ID,ACCT_NAME from ACCOUNT_ACT where ID=?";//自己寫
try {
for (int i = 0; i <10; i++) {
Connection conn=ConnectionUtils.getConnection(); //ConnectionUtils工具類在我的博客之中
//Connection conn=NormalConnectionUtils.getConnection();
PreparedStatement pstm=conn.prepareStatement(sql);
pstm.setInt(1, 1);
ResultSet rs=pstm.executeQuery();
while(rs.next()){
String accountID=rs.getString("ID");
String accountName=rs.getString("ACCT_NAME");
//System.out.println("accountID is "+accountID+" , accountName is "+accountName);
}
ConnectionUtils.close(rs, pstm);
//NormalConnectionUtils.close(rs, pstm, conn);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}/*finally{
ConnectionUtils.close(rs, pstm);
}*/
count++;
if(count==80){
long dateTime2=System.currentTimeMillis();
long result=dateTime2-dateTime1;
System.out.println("dateTime2 is "+dateTime2);
System.out.println(count+"個線程平均每個線程做10次訪問數(shù)據(jù)庫的中的時間為 " + result);
}
}
}
這個測試類是在我發(fā)表的前一條日志的基礎(chǔ)上做的,大家來點評點評。