1.jmeter 5.3版本及以上需要多添加一个oro-2.0.8.jar ,然后通用 ApacheJMeter_core.jar ApacheJMeter_java.jar
2.上代码:
public class InsertTest implements JavaSamplerClient{ public static void main(String[] args) { InsertTest insertTest = new Inserttest(); // Arguments arguments = new Arguments(); // JavaSamplerContext samplerContext = new JavaSamplerContext(arguments); Arguments arguments = insertTest.getDefaultParameters(); JavaSamplerContext samplerContext = new JavaSamplerContext(arguments); //增加一个菜篮子容器 insertTest.setupTest(samplerContext); insertTest.runTest(samplerContext); insertTest.teardownTest(samplerContext); } /* 声明全局变量 */ PreparedStatement ps; Connection conn; String sql = "insert into user (username,password,age)value (?,?,?)"; @Override public void setupTest(JavaSamplerContext javaSamplerContext) { //1. 注册驱动,告诉JDBC链接数据库的类型,传入MysqL驱动类型 try { Class.forName("com.MysqL.jdbc.Driver"); String dburl = javaSamplerContext.getParameter("dburl"); String dbusername = javaSamplerContext.getParameter("dbusername"); String dbpassword = javaSamplerContext.getParameter("dbpassword"); //2.建立链接 :IP 端口 用户名 密码数据库名 conn= DriverManager.getConnection(dburl, dbusername, dbpassword); //3. 对sql语句进行预编译 ps = conn.prepareStatement(sql); } catch (ClassNotFoundException | sqlException e) { e.printstacktrace(); } } @Override public SampleResult runTest(JavaSamplerContext javaSamplerContext) { SampleResult sampleResult = new SampleResult(); //结果对象 sampleResult.setSampleLabel("insert"); //给请求 取名 sampleResult.sampleStart(); //请求开始时间 //4 .对sql语句问好进行实参 try { String username = javaSamplerContext.getParameter("username"); String password = javaSamplerContext.getParameter("password"); int age = javaSamplerContext.getIntParameter("age"); ps.setString(1,"username"); ps.setString(2,"password"); ps.setInt(3,age); //5. 执行sql int row = ps.executeUpdate(); System.out.println("执行结果的行数为:"+ row); if(row == 1){ sampleResult.setSuccessful(true); }else { sampleResult.setSuccessful(false); } String res = "插入的元素" +row; sampleResult.setResponseData(res,"utf-8"); //给返回结果 } catch (sqlException e) { e.printstacktrace(); } sampleResult.sampleEnd(); //请求结束时间 return sampleResult; //返回请求结果 } @Override public void teardownTest(JavaSamplerContext javaSamplerContext) { //6. 关闭数据库链接 try { conn.close(); } catch (sqlException e) { e.printstacktrace(); } } @Override public Arguments getDefaultParameters() { // 初始化Java sampler 参数 Arguments arg = new Arguments(); arg.addArgument("dburl","http://localhost:8080/pinter"); arg.addArgument("dbusername","root"); arg.addArgument("dbpassword","123456"); arg.addArgument("username","chevron"); arg.addArgument("password","123456"); arg.addArgument("age","22"); return arg; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。