4.Jmeter-JDBC的使用

本文介绍如何使用JMeter进行数据库测试,包括配置JDBC连接、执行SQL查询并获取结果,以及如何将查询结果用于后续HTTP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在线程下或者http请求下(根据实际情况)新增jdbc connection Configuration

 

注意:测试数据库的时候,要加载相对应的mysql,jar包才能运行,加载数据库jar包,有两种方式,
首先要下载mysql-connector-java-bin.jar,下载地址:https://dev.mysql.com/downloads/connector/j/

第一种方法:把jar包放到jmeter/lib 下面,重启jmeter

第二种方法:jmeter-测试计划-add directory or jar to classpath,把jar包加载进来

 

2 jdbc connection Configuration配置讲解


Variable Name

数据库连接池的变量名,之后JDBC request可以通过选择不同的连接池名来选择不同的数据库连接,也就是说,jdbc connnection  Configuration 中的variable name要与jdbc requext中的variable name要一致

Database URL:

数据库连接地址:

mysql: jdbc:mysql://ip(数据库地址):3306/jia_erp

sqlserver: jdbc:sqlserver://数据库地址;instanceName=实例名;databaseName=数据库名称;

oracle: jdbc:oracle:thin:@数据库地址:1521:数据库名

JDBC Driver class

数据库驱动

Username

数据库连接用户名

Password

数据库连接密码

 3.新增JDBC Request

 

 

Variable Name

jdbc connnection  Configuration 中的variable name要与jdbc request中的variable name要一致

Variable Names

获取的数据结果存在Variable Names变量里

 

 

 

4 新增http 请求(需要测试的接口)

前提:例如我现在创建的http 请求接口的参数中有需要,从jdbc提取的数据,作为参数。

注意:这边需要添加一个debug sampler,方便我们调试脚本,以及查看所有变量结果;添加过程如图:(一定要放在所有http sampler最后,因为一个线程中,所有的流程是按照从上到下执行的)


1.调用步骤5.3中的Variable Names变量:${变量名},调用的时候用${变量名_1} 后面加上_1表示第一列的意思


执行脚本后,我们可以看到,执行sql后的结果,n_agent_id_# 表示我们获取到8条结果,1-8分别代表查询出来的结果。

这个有什么用呢?

1.方便我们在做业务接口中,可以做循环等。也可以通过查询出来的特定结果,直接定位到那个,1-8 可以用变量n代替,再加个计数器做循环接口,以后再拿实例做讲解。


2025-07-01 15:17:22,311 INFO o.j.r.JARSourceHTTP: Found cached repo 2025-07-01 15:17:22,536 INFO o.j.r.PluginManager: Plugins Status: [jpgc-plugins-manager=1.11, jmeter-core=5.6.3, jmeter-ftp=5.6.3, jmeter-http=5.6.3, jmeter-jdbc=5.6.3, jmeter-jms=5.6.3, jmeter-junit=5.6.3, jmeter-java=5.6.3, jmeter-ldap=5.6.3, jmeter-mail=5.6.3, jmeter-mongodb=5.6.3, jmeter-native=5.6.3, jmeter-tcp=5.6.3, jmeter-components=5.6.3] 2025-07-01 15:17:32,191 INFO o.a.j.s.FileServer: Default base='D:\日常软件\H.Jmeter\apache-jmeter-5.6.3\bin' 2025-07-01 15:17:32,193 INFO o.a.j.g.a.Load: Loading file: D:\日常软件\H.Jmeter\查看结果树.jmx 2025-07-01 15:17:32,193 INFO o.a.j.s.FileServer: Set new base='D:\日常软件\H.Jmeter' 2025-07-01 15:17:32,240 INFO o.a.j.s.SaveService: Testplan (JMX) version: 2.2. Testlog (JTL) version: 2.2 2025-07-01 15:17:32,240 INFO o.a.j.s.SaveService: Using SaveService properties file encoding UTF-8 2025-07-01 15:17:32,240 INFO o.a.j.s.SaveService: Using SaveService properties version 5.0 2025-07-01 15:17:32,256 INFO o.a.j.s.SaveService: Loading file: D:\日常软件\H.Jmeter\查看结果树.jmx 2025-07-01 15:17:32,319 INFO o.a.j.p.h.s.HTTPSamplerBase: Parser for text/html is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser 2025-07-01 15:17:32,319 INFO o.a.j.p.h.s.HTTPSamplerBase: Parser for application/xhtml+xml is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser 2025-07-01 15:17:32,319 INFO o.a.j.p.h.s.HTTPSamplerBase: Parser for application/xml is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser 2025-07-01 15:17:32,319 INFO o.a.j.p.h.s.HTTPSamplerBase: Parser for text/xml is org.apache.jmeter.protocol.http.parser.LagartoBasedHtmlParser 2025-07-01 15:17:32,319 INFO o.a.j.p.h.s.HTTPSamplerBase: Parser for text/vnd.wap.wml is org.apache.jmeter.protocol.http.parser.RegexpHTMLParser 2025-07-01 15:17:32,319 INFO o.a.j.p.h.s.HTTPSamplerBase: Parser for text/css is org.apache.jmeter.protocol.http.parser.CssParser 2025-07-01 15:17:32,557 INFO o.a.j.s.SampleResult: Note: Sample TimeStamps are START times 2025-07-01 15:17:32,557 INFO o.a.j.s.SampleResult: sampleresult.default.encoding is set to UTF-8 2025-07-01 15:17:32,557 INFO o.a.j.s.SampleResult: sampleresult.useNanoTime=true 2025-07-01 15:17:32,557 INFO o.a.j.s.SampleResult: sampleresult.nanoThreadSleep=5000 2025-07-01 15:17:32,636 INFO o.a.j.r.ClassFinder: Will scan jar D:\日常软件\H.Jmeter\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-manager-1.11.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.visualizers.ResultRenderer], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-07-01 15:17:32,636 INFO o.a.j.r.ClassFinder: Will scan jar D:\日常软件\H.Jmeter\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-manager-1.11.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.visualizers.RequestView], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-07-01 15:17:32,668 INFO o.a.j.s.FileServer: Set new base='D:\日常软件\H.Jmeter' 2025-07-01 15:17:37,105 INFO o.a.j.e.StandardJMeterEngine: Running the test! 2025-07-01 15:17:37,105 INFO o.a.j.s.SampleEvent: List of sample_variables: [] 2025-07-01 15:17:37,105 INFO o.a.j.s.SampleEvent: List of sample_variables: [] 2025-07-01 15:17:37,105 INFO o.a.j.e.u.CompoundVariable: Note: Function class names must contain the string: '.functions.' 2025-07-01 15:17:37,105 INFO o.a.j.e.u.CompoundVariable: Note: Function class names must not contain the string: '.gui.' 2025-07-01 15:17:37,254 INFO o.a.j.r.ClassFinder: Will scan jar D:\日常软件\H.Jmeter\apache-jmeter-5.6.3\lib\ext\jmeter-plugins-manager-1.11.jar with filter ExtendsClassFilter [parents=[interface org.apache.jmeter.functions.Function], inner=false, contains=null, notContains=null]. Consider exposing JMeter plugins via META-INF/services, and add JMeter-Skip-Class-Scanning=true manifest attribute so JMeter can skip classfile scanning 2025-07-01 15:17:37,254 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*) 2025-07-01 15:17:37,254 INFO o.a.j.e.StandardJMeterEngine: Starting setUp thread groups 2025-07-01 15:17:37,254 INFO o.a.j.e.StandardJMeterEngine: Starting setUp ThreadGroup: 1 : tbd 线程组 2025-07-01 15:17:37,254 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group tbd 线程组. 2025-07-01 15:17:37,254 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error 2025-07-01 15:17:37,254 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false 2025-07-01 15:17:37,270 INFO o.a.j.t.ThreadGroup: Started thread group number 1 2025-07-01 15:17:37,270 INFO o.a.j.e.StandardJMeterEngine: Waiting for all setup thread groups to exit 2025-07-01 15:17:37,270 INFO o.a.j.t.JMeterThread: Thread started: tbd 线程组 1-1 2025-07-01 15:17:37,270 INFO o.a.j.p.h.s.HTTPHCAbstractImpl: Local host = jizhi 2025-07-01 15:17:37,285 INFO o.a.j.p.h.s.HTTPHC4Impl: HTTP request retry count = 0 2025-07-01 15:17:37,424 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``// JSR223 Sampler 中使用 import org.apache.http.client.methods.HttpPost; import org . . . '' : Typed variable declaration : Object constructor 2025-07-01 15:17:37,424 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``// JSR223 Sampler 中使用 import org.apache.http.client.methods.HttpPost; import org . . . '' : Typed variable declaration : Object constructor 2025-07-01 15:17:37,460 INFO o.a.j.t.JMeterThread: Thread is done: tbd 线程组 1-1 2025-07-01 15:17:37,460 INFO o.a.j.t.JMeterThread: Thread finished: tbd 线程组 1-1 2025-07-01 15:17:37,475 INFO o.a.j.e.StandardJMeterEngine: All Setup Threads have ended 2025-07-01 15:17:37,539 INFO o.a.j.e.StandardJMeterEngine: No enabled thread groups found 2025-07-01 15:17:37,539 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2025-07-01 15:17:37,539 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值