JMeter & JDBC
CSV data is the default data source used in Apache JMeter but there may be circumstances where data may be better sourced from a database. This can be achieved through the use of a JDBC sampler.
Within JMeter there are two JDBC components; i) JDBC Connection Configuration and ii) JDBC Request.
JDBC Connection Configuration
There are a plethora of options under the connection configuration component but you can leave many of the defaults and simply look to populate the following with the appropriate values (examples populated below):-
Variable name for created pool = MySQLDB
Database URL = jdbc:mysql://
JDBC Driver class = com.mysql.jdbc.Driver
Username = root
Password = <password>
Apache JMeter will then create a single connection to the database with the provided values. This connection can then be used throughout the test plan.
JDBC Request
The JDBC Request sampler allows you to query the database connection defined in the JDBC connection configuration component. To use the existing connection you will need to supply:-
Variable Name of Pool declared in JDBC Connection Configuration = MySQLDB
Query Type = Select Statement
Variable names = count
Result Variable name = Request_1
Handle ResultSet = Count Records
In this example we simply query the database for a count from the dummydata table.
SELECT COUNT(*) FROM dummydata;
The count will be returned and stored in the $count variable which can then be used within the test plan.
Query Type
The JDBC Request supports a number of test types which are listed below:-
Select Statement
Update Statement - use this for Inserts and Deletes as well
Callable Statement
Prepared Select Statement
Prepared Update Statement - use this for Inserts and Deletes as well
Commit
Rollback
AutoCommit (false)
AutoCommit (true)
Handle ResultSet
There are also a number of methods to handle result sets which are listed below:-
Count Records - Variables of ResultSet types will be iterated through showing the count of records as result. Variables will be stored as Strings.
Store as String - All variables on Variable Names list are stored as strings,
Store as Object - variables of ResultSet type on Variables Names list will be stored as Object and can be accessed in subsequent tests/scripts and iterated, will not iterate through the ResultSet.