jmeter 连接mysql数据库
配置数据库连接
配置连接参数
Variable Name for created pool:填入一个变量名,在jdbc request中可以通过这个名称选择合适的连接池进行使用。
Max Number of connections : 最大连接数
MAX wait :最大等待时间
Auto Commit: 是否自动提交事务
DataBase URL : 数据库连接地址 jdbc:mysql://host:port/service
JDBC Driver Class : 数据库驱动,选择对应的mysql
username:数据库用户名
password:数据库密码
新建jdbc request
Parameter values 变量值;多个变量用,隔开
Parameter types 变量类型
Query timeout(s) 超时时间
Limit ResultSet 同limit
Query Type sql 语句的类型
1.select statement :Select Statement:仅支持测试select语句,并且一次只能测试一条
2.UpdateStatement:支持测试非select语句(增删改),并且只能测试一条;若其中夹杂select语句,自动忽略;若第一条语句为select语句,报错;
3.Callable Statement:用于存储过程和方法,只要语法正确,任何语句,再多的条数都能支持
4.prepared update statement及prepared select statement :用于为一条SQL语句生成执行计划,可只更换变量重用
5.Commit :将存储的SQL语句结果写入数据库表
6.Rollback :撤销指定sql语句的过程
7.AutoCommit(false) :自动提交模式 将用户操作一直处于某个事务中,直到执行一条commit提交或rollback语句才会结束当前事务重新开始一个新的事务。
8.AutoCommit(true) : 无论什么情况 都自动提交将结果写入,结束当前事务开始下一个事务
9.编辑(${}) :实现同时多个不同用户使用不同的SQL,可以通过把整条SQL语句参数化来实现;(把SQL语句放在csv文件中,然后在JDBC Request的Query 中使用参数代替 ${SQL_Statement})。
(1)Query Type为Select Statement时,对应执行代码为:
stmt = conn.createStatement();
ResultSet rs = null;
rs = stmt.executeQuery(sql);
return getStringFromResultSet(rs).getBytes(ENCODING);
(2)Query Type为Callable Statement时,对应执行代码为:
CallableStatement cstmt = getCallableStatement(conn);
int out[]=setArguments(cstmt);
boolean hasResultSet = cstmt.execute();
String sb = resultSetsToString(cstmt,hasResultSet, out);
return sb.getBytes(ENCODING);
(3)Query Type为Update Statement时,对应执行代码为:
stmt = conn.createStatement();
stmt.executeUpdate(sql);
int updateCount = stmt.getUpdateCount();
String results = updateCount + " updates";
return results.getBytes(ENCODING);
(4)Query Type为Prepared Select Statement时,对应执行代码为:
PreparedStatement pstmt = getPreparedStatement(conn);
setArguments(pstmt);
ResultSet rs = null;
rs = pstmt.executeQuery();
return getStringFromResultSet(rs).getBytes(ENCODING);
(5)Query Type为Prepared Update Statement时,对应执行代码为:
PreparedStatement pstmt = getPreparedStatement(conn);
setArguments(pstmt);
pstmt.executeUpdate();
String sb = resultSetsToString(pstmt,false,null);
return sb.getBytes(ENCODING);
(6)Query Type为Rollback时,对应执行代码为:
conn.rollback();
return ROLLBACK.getBytes(ENCODING);
(7)Query Type为Commit时,对应执行代码为:
conn.commit();
return COMMIT.getBytes(ENCODING);
(8)Query Type为AutoCommit(false)时,对应执行代码为:conn.setAutoCommit(false);
return AUTOCOMMIT_FALSE.getBytes(ENCODING);
(9)Query Type为AutoCommit(true)时,对应执行代码为:
conn.setAutoCommit(true);
return AUTOCOMMIT_TRUE.getBytes(ENCODING);
(10)其它情况直接抛异常:throw new UnsupportedOperationException("Unexpected query type: "+_queryType);