62,634
社区成员




import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleCallableStatement;
public class TestCall {
private static int TIMES = 1000;
private static String URL = "jdbc:oracle:thin:@192.168.234.128:1521:redhat5";
private static String USER = "zenki";
private static String PASS = "zenki";
public static Connection openConnection() throws SQLException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
return DriverManager.getConnection(URL, USER, PASS);
}
public static void main(String[] args) throws Exception {
Connection con = openConnection();
String sql = "call proc_test(?)";
// 预跑
SimpleCaller.callProc(con, sql, "");
// 性能测试
long timer = System.currentTimeMillis();
for (int i = 0; i < TIMES; i++) {
SimpleCaller.callProc(con, sql, String.valueOf(i));
}
timer = System.currentTimeMillis() - timer;
System.out.println("\nTime spend: " + timer + "\t Per: " + (1.0 * timer / TIMES));
con.close();
}
}
class SimpleCaller {
public static String callProc(Connection con, String sql, String param) throws SQLException {
OracleCallableStatement cstmt = (OracleCallableStatement) con.prepareCall(sql);
try {
cstmt.registerOutParameter(1, Types.VARCHAR);
cstmt.setString(1, String.valueOf(param));
cstmt.execute();
return cstmt.getString(1);
} finally {
cstmt.close();
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleCallableStatement;
public class TestCall {
private static int TIMES = 1000;
private static int THREAD_NUM = 100;
private static String URL = "jdbc:oracle:thin:@192.168.234.128:1521:redhat5";
private static String USER = "zenki";
private static String PASS = "zenki";
public static Connection openConnection() throws SQLException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
return DriverManager.getConnection(URL, USER, PASS);
}
public static void main(String[] args) throws Exception {
Connection con = openConnection();
final String sql = "call proc_test(?)";
// 预跑
SimpleCaller.callProc(con, sql, "");
// 单线程性能测试
long timer = System.currentTimeMillis();
for (int i = 0; i < TIMES; i++) {
SimpleCaller.callProc(con, sql, String.valueOf(i));
}
timer = System.currentTimeMillis() - timer;
System.out.println("\nTime spend: " + timer + "\t Per: " + (1.0 * timer / TIMES));
// 多线程准备
Thread[] threads = new Thread[THREAD_NUM];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread("T" + i) {
public void run() {
try {
Connection con = openConnection();
for (int i = 0; i < TIMES; i++) {
SimpleCaller.callProc(con, sql, String.valueOf(i));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
};
}
// 多线程性能测试
timer = System.currentTimeMillis();
for (int i = 0; i < threads.length; i++) {
threads[i].start();
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
timer = System.currentTimeMillis() - timer;
System.out.println("MulitThread done. Time spend: " + timer + "\t Per: " + (1.0 * timer / TIMES / THREAD_NUM)
+ "\t ThreadNum: " + THREAD_NUM);
}
}
class SimpleCaller {
public static String callProc(Connection con, String sql, String param) throws SQLException {
OracleCallableStatement cstmt = (OracleCallableStatement) con.prepareCall(sql);
try {
cstmt.registerOutParameter(1, Types.VARCHAR);
cstmt.setString(1, String.valueOf(param));
cstmt.execute();
return cstmt.getString(1);
} finally {
cstmt.close();
}
}
}