How to parameterize the database connection in JMeter?
To connect to a database and use parameterization in JMeter, you can follow these steps:
- Add a JDBC driver: Place the database’s JDBC driver (such as mysql-connector-java.jar) in the lib directory of JMeter.
- Add a JDBC connection configuration element: Right-click in the test plan, select “Add” -> “Config Element” -> “JDBC Connection Configuration”. Enter a variable name in the “Variable Name” field under the “JDBC Connection Configuration” element, which will be used to store the database connection.
- Set up JDBC connection properties: In the “JDBC Connection Configuration Component,” input the database’s connection URL in the “Database URL” field, for example: jdbc:mysql://localhost:3306/database_name. Next, enter the database username and password.
- Add a JDBC Sampler: Right-click in the test plan, select “Add” -> “Sampler” -> “JDBC Request”. Enter a variable name in the “Variable Name” field of the “JDBC Request” component to store the query result.
- Configure JDBC request properties: In the “JDBC Request” component, select the JDBC connection configuration element created earlier and fill in the SQL query statement. Variables can be used to replace parameters in the query, for example: SELECT * FROM table_name WHERE id = ${id}.
- Add a CSV data file configuration component: Right-click in the test plan, select “Add” -> “Config Element” -> “CSV Data Set Config”. In the “CSV Data Set Config” component, choose a CSV file that contains the values to be used for parameterization.
- Configure the properties of the CSV data file: In the “CSV Data File Configuration” element, set the “Variable Name” field to be the same as the parameter name to replace in the JDBC request, and set the “File Name” field to the path of the CSV file.
- Add a loop controller: Right-click in the test plan, select “Add” -> “Logic Controller” -> “Loop Controller”. Within the loop controller, set the number of loops to perform multiple queries for each parameterized value.
- Run the test plan: Save your test plan and execute it. JMeter will run database queries for each parameterized value and store the query results in their respective variables.
By doing this, you can use parameters to connect to the database and execute queries. It’s important to note that you can add additional components and configuration properties as needed.