6.Benchmark SQL 数据库测试工具代码——执行SQL类

6.Benchmark SQL 数据库测试工具代码——执行SQL类

欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/50357482

执行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

 

 

 staticvoidexecJDBC(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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值