6.Benchmark SQL 数据库测试工具代码——执行SQL类
欢迎转载,转载请标明出处:blog.csdn.net/notbaron/ar…\
执行SQL类是ExecJDBC.两个函数main和execJDBC函数。
1. main
Main 函数获取属性文件中的属性值,如:
prop
driver
conn
user
password
该类在执行的时候会输入一个包含SQL语句的文本。
然后逐行读入,直到碰到”;”, 就调用execJDBC函数进行执行。
2. execJDBC
函数execJDBC,完成调用执行SQL函数。
3. 源码
import java.io.*;
import java.sql.*;
import java.util.*;
publicclassExecJDBC {
publicstaticvoidmain(String[]args){
Connection conn= null;
Statement stmt= null;
String rLine= null;
StringBuffer sql= newStringBuffer();
try{
Properties ini= newProperties();
ini.load(newFileInputStream(System.getProperty("prop")));
// Register jdbcDriver
Class.forName(ini.getProperty("driver" ));
// make connection
conn= DriverManager.getConnection(ini.getProperty("conn"),
ini.getProperty("user"),ini.getProperty("password"));
conn.setAutoCommit(true);
// Create Statement
stmt=conn.createStatement();
// Open inputFile
BufferedReader in = newBufferedReader
(new FileReader(jTPCCUtil.getSysProp("commandFile",null)));
// loop thru input file and concatenate SQLstatement fragments
while((rLine =in.readLine()) != null) {
String line = rLine.trim();
if (line.length() != 0) {
if (line.startsWith("--")) {
System.out.println(line); // printcomment line
} else {
sql.append(line);
if (line.endsWith(";")) {
execJDBC(stmt,sql);
sql = new StringBuffer();
} else {
sql.append("\n");
}
}
} //end if
} //end while
in.close();
} catch(IOExceptionie) {
System.out.println(ie.getMessage());
} catch(SQLExceptionse) {
System.out.println(se.getMessage());
} catch(Exceptione) {
e.printStackTrace();
//exit Cleanly
} finally{
try{
if (conn !=null)
conn.close();
} catch(SQLExceptionse) {
se.printStackTrace();
} // end finally
} // end try
} // end main
staticvoid execJDBC(Statementstmt, StringBuffer sql){
System.out.println(sql);
try{
stmt.execute(sql.toString().replace(';',' '));
}catch(SQLExceptionse) {
System.out.println(se.getMessage());
} // end try
} // end execJDBCCommand
} // end ExecJDBC Class