JMeter and JDBC
Guide to using JDBC samplers in Apache JMeter for sourcing test data from databases instead of CSV files.
Mark
Performance Testing Expert
Apache JMeter can query databases directly using JDBC components rather than relying on CSV data sources. This approach is useful when test data is already in a database or when you need dynamic data during test execution.
JDBC Components
Two main components are required:
- JDBC Connection Configuration - Establishes the database connection
- JDBC Request - Executes queries against the database
JDBC Connection Configuration
This component establishes the database connection pool. Add it to your test plan before the JDBC Request sampler.
Key Configuration Fields
| Field | Example Value | Description |
|---|---|---|
| Variable Name | MySQLDB | Name for the connection pool |
| Database URL | jdbc:mysql://localhost:3306/testdb | JDBC connection string |
| JDBC Driver class | com.mysql.jdbc.Driver | Database driver class |
| Username | testuser | Database username |
| Password | ******** | Database password |
Common JDBC URLs
# MySQL
jdbc:mysql://hostname:3306/database
# PostgreSQL
jdbc:postgresql://hostname:5432/database
# Oracle
jdbc:oracle:thin:@hostname:1521:SID
# SQL Server
jdbc:sqlserver://hostname:1433;databaseName=database
JMeter creates a single persistent connection pool that can be reused throughout the test plan.
JDBC Request Sampler
The JDBC Request component queries the configured database.
Required Fields
| Field | Value | Description |
|---|---|---|
| Variable Name of Pool | MySQLDB | Must match Connection Configuration |
| Query Type | Select Statement | Type of SQL operation |
| Variable names | count | Variable to store result |
| Result Variable name | Request_1 | Name for the ResultSet |
| Handle ResultSet | Count Records | How to process results |
Example Query
SELECT COUNT(*) FROM dummydata;
The count result is stored in the ${count} variable for subsequent test plan use.
Query Type Options
Eight query types are supported:
| Query Type | Use Case |
|---|---|
| Select Statement | Read data |
| Update Statement | INSERT, UPDATE, DELETE operations |
| Callable Statement | Stored procedures |
| Prepared Select Statement | Parameterized SELECT |
| Prepared Update Statement | Parameterized INSERT/UPDATE/DELETE |
| Commit | Explicit commit |
| Rollback | Explicit rollback |
| AutoCommit(false) | Disable auto-commit |
| AutoCommit(true) | Enable auto-commit |
Handle ResultSet Methods
Three approaches for processing results:
Count Records
Iterates through variables, storing values as strings. Each column becomes a variable:
${column1_1}, ${column1_2}, ${column1_3}...
Store as String
All variables stored as strings with column names:
${Result_1} contains the full result as a string
Store as Object
ResultSet variables stored as objects for manual iteration without automatic traversal. Useful when you need programmatic access:
// In BeanShell/JSR223
vars.getObject("Result_1")
Complete Example
Here’s a complete setup for reading user data:
Test Plan
+ JDBC Connection Configuration (MySQLDB)
+ Thread Group
+ JDBC Request
Variable Name of Pool: MySQLDB
Query Type: Select Statement
Query: SELECT user_id, username FROM users LIMIT 100
Variable names: user_id,username
Result Variable name: Users
Handle ResultSet: Count Records
+ HTTP Request
Use ${user_id_1}, ${username_1} etc.
Driver Setup
Ensure the JDBC driver JAR is in JMeter’s lib folder:
# Copy driver to JMeter lib directory
cp mysql-connector-java-8.0.x.jar $JMETER_HOME/lib/
Further Reading
Tags: