第一步:添加lib
第二步:把lib添加到工程的环境变量中
增删改数据
package test1;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class test_setData {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1、注册驱动,告诉jdbc链接数据库的类型,传入MySQL的驱动类名
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://{ip}:{port}/{dbname}?useUnicode=true&characterEncoding=utf8";
String username = "";
String password = "";
String sql = "";
// 2、通过ip、端口号、数据库名、用户名、密码,建立连接,返回一个链接对象Connection
Connection conn = DriverManager.getConnection(url,username,password);
//3、对SQL语句进行预编译,提升执行性能
PreparedStatement ps = conn.prepareStatement(sql);
/*
* 如果SQL = "UPDATE user_identity SET name='?' where user_id = ?"
* 其中有问号,则需要对?进行赋值
* ps.setString(1,"test1);
* ps.setInt(2,123456);
* */
//4、执行SQL,增删改用executeLargeUpdate;查用executeQuery()
//增删改
ps.executeLargeUpdate(sql);
/*查
ResultSet set = ps.executeQuery();
while (set.next()){
String name = set.getString("name");
System.out.println(name);
}
*/
// 5、关闭数据库连接,以防数据库崩溃
conn.close();
}
}
第一步:写代码
需改造上一节中的代码,以适应在JMeter中跑java请求
先继承JavaSamplerClient中的方法,通过图一中的位置可选择需要继承的接口
package test1;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.sql.*;
public class test_JmeterOnJava implements JavaSamplerClient {
public static void main(String[] args) {
Arguments arguments = new Arguments();
JavaSamplerContext context = new JavaSamplerContext(arguments);
test_JmeterOnJava test = new test_JmeterOnJava();
test.setupTest(context);
test.runTest(context);
test.teardownTest(context);
}
String url = "jdbc:mysql://{ip}:{port}/{dbname}?useUnicode=true&characterEncoding=utf8";
String username = "";
String password = "";
// String sql = "UPDATE user_identity SET name='zyt186药剂师' where user_id = 38602938";
String sql = "select * from where ....";
Connection conn;
PreparedStatement ps;
/**
* 初始化函数,每个线程先执行此函数,并且仅执行一次
*/
@Override
public void setupTest(JavaSamplerContext javaSamplerContext) {
try {
// 1、注册驱动,告诉jdbc链接数据库的类型,传入MySQL的驱动类名
Class.forName("com.mysql.cj.jdbc.Driver");
// 2、通过ip、端口号、数据库名、用户名、密码,建立连接,返回一个链接对象Connection
//3、对SQL语句进行预编译,提升执行性能
conn = DriverManager.getConnection(url, username, password);
ps = conn.prepareStatement(sql);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* 执行业务代码函数,在setupTest方法后执行,每个线程执行N次
*/
@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sampleResult = new SampleResult();
// 设置请求名字
sampleResult.setSampleLabel("select");
ResultSet set = null;
// 设置请求开始、结束时间,是为了拿到请求响应的时间
//请求开始
sampleResult.sampleStart();
try {
set = ps.executeQuery();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
//请求结束
sampleResult.sampleEnd();
while (true) {
try {
if (!set.next()) break;
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
String name = null;
try {
name = set.getString("name");
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
System.out.println(name);
sampleResult.setSuccessful(true);
}
return sampleResult;
}
/**
* 结束函数,在脚本运行结束时执行,每个线程仅执行一次
*/
@Override
public void teardownTest(JavaSamplerContext javaSamplerContext) {
// 5、关闭数据库连接,以防数据库崩溃
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Arguments getDefaultParameters() {
return null;
}
}
报错
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: org/apache/oro/text/regex/Perl5Matcher
at org.apache.jmeter.util.JMeterUtils.<clinit>(JMeterUtils.java:141)
at org.apache.jmeter.samplers.SampleResult.<clinit>(SampleResult.java:77)
at test1.test_JmeterOnJava.runTest(test_JmeterOnJava.java:50)
at test1.test_JmeterOnJava.main(test_JmeterOnJava.java:17)
Caused by: java.lang.NoClassDefFoundError: org/apache/oro/text/regex/Perl5Matcher
... 4 more
Caused by: java.lang.ClassNotFoundException: org.apache.oro.text.regex.Perl5Matcher
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
遇见的错误:
1、Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
解决:更改jdbc.properties配置 .com.mysql.jdbc.Driver 改为 com.mysql.cj.jdbc.Driver
2、可能会出现【缺少:org.apache.oro.text.regex.Perl5Matcher】的问题,导入下面的oro jar包就可以解决啦
jar包下载https://www.aliyundrive.com/s/UvKY7eygktD
第二步:导出脚本为jar包
1、file→project structure→artiacts→JAR→from modules with dependencies
2、main class中选择需要导出的脚本
3、一直保存就ok了
4、选择build→build artifacts
out文件夹下可找到导出的jar包
5、jar包放在 \apache-jmeter-5.5\bin\ext 下,重启Jmeter,java请求中选择导入的jar包名
6、成功!
cmd中会打印代码中的
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.sql.*;
public class test_JavaOnJmeter implements JavaSamplerClient {
String sql = "select * from user_identity where user_id = ?";
Connection conn;
PreparedStatement ps;
public static void main(String[] args) {
//模拟jmeter执行一次脚本
test_JavaOnJmeter test = new test_JavaOnJmeter();
JavaSamplerContext context = new JavaSamplerContext(test.getDefaultParameters());
test.setupTest(context);
test.runTest(context);
test.teardownTest(context);
}
/**
* 初始化函数,每个线程先执行此函数,并且仅执行一次
*/
@Override
public void setupTest(JavaSamplerContext javaSamplerContext) {
try {
// 1、注册驱动,告诉jdbc链接数据库的类型,传入MySQL的驱动类名
Class.forName("com.mysql.cj.jdbc.Driver");
// 2、通过ip、端口号、数据库名、用户名、密码,建立连接,返回一个链接对象Connection
String url = javaSamplerContext.getParameter("url");
System.out.println(url);
String dbUserName = javaSamplerContext.getParameter("dbUsername");
System.out.println(dbUserName);
String dbPassword = javaSamplerContext.getParameter("dbPassword");
System.out.println(dbPassword);
//3、对SQL语句进行预编译,提升执行性能
conn = DriverManager.getConnection(url,dbUserName,dbPassword);
ps = conn.prepareStatement(sql);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
* 执行业务代码函数,在setupTest方法后执行,每个线程执行N次
*/
@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
SampleResult sampleResult = new SampleResult();
// 设置请求名字
sampleResult.setSampleLabel("select");
ResultSet set = null;
// 设置请求开始、结束时间,是为了拿到请求响应的时间
//请求开始
sampleResult.sampleStart();
try {
int userId = javaSamplerContext.getIntParameter("userId");
ps.setInt(1,userId);
set = ps.executeQuery();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
//请求结束
sampleResult.sampleEnd();
while (true) {
try {
if (!set.next()) break;
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
String name = null;
try {
name = set.getString("name");
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
System.out.println(name);
sampleResult.setSuccessful(true);
}
return sampleResult;
}
/**
* 结束函数,在脚本运行结束时执行,每个线程仅执行一次
*/
@Override
public void teardownTest(JavaSamplerContext javaSamplerContext) {
// 5、关闭数据库连接,以防数据库崩溃
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Arguments getDefaultParameters() {
Arguments arguments = new Arguments();
arguments.addArgument("url","");
arguments.addArgument("dbUsername","");
arguments.addArgument("dbPassword","");
arguments.addArgument("userId","");
return arguments;
}
}