import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class SQLTEST {
?
?public static void procedureTest(String tables,int num) {
??try{
???Connection conn = DriverManager.getConnection("jdbc:default:connection");
???Statement stmt = conn.createStatement();
???StringTokenizer stk = new StringTokenizer(tables,",");
???while(stk.hasMoreTokens()) {
????String table = stk.nextToken();
????String sql = "SELECT * FROM " + table;
????for(int i=0;i<num;i++) {
?????stmt.executeQuery(sql);
?????
????}
???}
???stmt.close();
???conn.close();
??}catch(Exception e) {
???e.printStackTrace();
??}
?}
}
將此class編譯后拷貝到DB2安裝目錄的function目錄下。
再用servlet來(lái)調(diào)用它:
package com.test;
import java.io.IOException;
import java.util.StringTokenizer;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SQLServlet extends HttpServlet {
?public void doGet(HttpServletRequest req, HttpServletResponse resp)
??throws ServletException, IOException {
???doPost(req,resp);
?}
?public void doPost(HttpServletRequest req, HttpServletResponse resp)
??throws ServletException, IOException {
???CallableStatement callStmt = null;
???Connection conn = null;
???Statement stmt = null;
???String tables = "shgt_auth,shgt_master,trx_inbox";
???long start = 0;
???long end = 0;
???long lastTime = 0;
???int num = 100;
???try {
????
????//存儲(chǔ)過(guò)程調(diào)用
????conn = getConnection("CET","eximtrx","Standard00");
????String procName = "SQLTEST";
????String sql = "CALL " + procName + "(?,?)";
????callStmt = conn.prepareCall(sql);
????callStmt.setString(1,tables);
????callStmt.setInt(2,num);
????start = System.currentTimeMillis();
????callStmt.execute();
????end = System.currentTimeMillis();
????lastTime = end - start;
????System.out.println("###########java store procedure Last time : " + lastTime/1000? + "s" + lastTime%1000 + "ms");
????
????
????
????//SQL 調(diào)用
????conn = getConnection("CET","eximtrx","Standard00");
????stmt = conn.createStatement();
????StringTokenizer strTok = new StringTokenizer(tables,",");
????start = System.currentTimeMillis();
????while(strTok.hasMoreTokens()) {
?????String table = strTok.nextToken();
?????String sql2 = "select * from " + table;
?????for(int i=0;i<num;i++){
??????stmt.executeQuery(sql2);
?????}
????}
????end = System.currentTimeMillis();
????lastTime = end - start;
????System.out.println("###########SQL Last time : " + lastTime/1000? + "s" + lastTime%1000 + "ms");
???} catch (Exception e) {
????e.printStackTrace();
???}
?}
?private Connection getConnection(String ds, String user, String pwd)
? throws Exception {
?? Connection con = null;
?? InitialContext ctx = new InitialContext();
?? DataSource fDS = (DataSource)ctx.lookup(ds);
?? con = fDS.getConnection(user, pwd);
?? return con;
?}
}
運(yùn)行此servlet就可成功調(diào)用存儲(chǔ)過(guò)程。
其中有個(gè)注意點(diǎn),就是大小寫(xiě)的問(wèn)題,剛開(kāi)始時(shí)建存儲(chǔ)過(guò)程是這樣CREATEPROCEDURE SQLTest(IN tables VARCHAR(200),IN num INTEGER) ,java class也用的是SQLTest,但是在調(diào)存儲(chǔ)過(guò)程時(shí)會(huì)抱錯(cuò),說(shuō)找不到SQLTEST.procedureTest方法,原來(lái)數(shù)據(jù)庫(kù)在創(chuàng)建存儲(chǔ)過(guò)程時(shí),即使你使用SQLTest,但也會(huì)被執(zhí)行為SQLTEST,所以只有都改成大寫(xiě)。
但有點(diǎn)意外的是,通過(guò)這個(gè)測(cè)試出來(lái)的數(shù)據(jù),存儲(chǔ)過(guò)程既然比sql跑得慢,不知道為什么,難道程序有問(wèn)題?請(qǐng)大家看一下