Skip to main content
Back to blog
tutorials 24 March 2020 3 min read

JMeter and JDBC

Guide to using JDBC samplers in Apache JMeter for sourcing test data from databases instead of CSV files.

M

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:

  1. JDBC Connection Configuration - Establishes the database connection
  2. 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

FieldExample ValueDescription
Variable NameMySQLDBName for the connection pool
Database URLjdbc:mysql://localhost:3306/testdbJDBC connection string
JDBC Driver classcom.mysql.jdbc.DriverDatabase driver class
UsernametestuserDatabase 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

FieldValueDescription
Variable Name of PoolMySQLDBMust match Connection Configuration
Query TypeSelect StatementType of SQL operation
Variable namescountVariable to store result
Result Variable nameRequest_1Name for the ResultSet
Handle ResultSetCount RecordsHow 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 TypeUse Case
Select StatementRead data
Update StatementINSERT, UPDATE, DELETE operations
Callable StatementStored procedures
Prepared Select StatementParameterized SELECT
Prepared Update StatementParameterized INSERT/UPDATE/DELETE
CommitExplicit commit
RollbackExplicit 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:

#jmeter #jdbc #database #performance-testing #mysql

Need help with performance testing?

Let's discuss how I can help improve your application's performance.

Get in Touch