近期通过数据库压力测试,看了一些资料,方法都差不多。
1、下载JDBC驱动(sqljdbc4.jar)
2、在run_time setting下的classpath把这驱动引入。
3、编写及哦啊本
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*
*/
import lrapi.lr;
import java.io.*;
import java.sql.*;
import lrapi.web;
public class Actions
{
int sum=0;
int ColumnCount=0;
//String dbserverip = lr.eval_string("<dbserver>");
//定义数据库连接串
String conURL="jdbc:sqlserver://xx.xx.x.xx\\sql1;DatabaseName=NewDB";
//连接数据库用户名
String user="prj_tmp_rw";
//数据库口令
String password="xxxxx";
Statement stat;
ResultSet Result1;
Connection conn;
public int init() throws Throwable {
//加载JDBC驱动
lr.think_time(5);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("驱动加载完成");
//连接数据库
lr.think_time(5);
conn = DriverManager.getConnection(conURL,user,password);
lr.think_time(5);
stat=conn.createStatement();
return 0;
}//end of init
public int action() throws Throwable {
lr.think_time(5);
lr.start_transaction("lr_Query_data_trans");
Result1=stat.executeQuery("<QueryParm>");
ResultSetMetaData rsmd = Result1.getMetaData();
ColumnCount = rsmd.getColumnCount();
System.out.println("结果集的列数:" + ColumnCount);
if(ColumnCount==0)
{
lr.end_transaction("lr_Query_data_trans",lr.FAIL);
}
else
{
lr.end_transaction("lr_Query_data_trans",lr.PASS);
}
return 0;
}//end of action
public int end() throws Throwable {
Result1.close();
stat.close();
conn.close();
return 0;
}//end of end
}