Learn how to use the JSR223 sampler to store SQL query results into a database table
P粉633309801
P粉633309801 2023-08-29 22:13:53
0
1
463
<p>Please provide step-by-step process</p> <p>How to use jsr223 sampler to connect to MySQL database and execute queries, and store the results in the database table? Please provide sample code for this theme</p>
P粉633309801
P粉633309801

reply all(1)
P粉302484366
  1. DownloadMySQL JDBC Driver and put it into the "lib" folder under the JMeter installation directory (or put it into other files in the JMeter classpath folder)

  2. Restart JMeter to load the .jar file

  3. Add Thread Group to your test plan

  4. Add JSR223 Sampler to your thread group

  5. Put the following code into the "Script" area:

    import groovy.sql.Sql
    
    def url = 'jdbc:mysql://your-database-host:your-database-port/your-database-name'
    def user = 'your-username'
    def password = 'your-password'
    def driver = 'com.mysql.cj.jdbc.Driver'
    def sql = Sql.newInstance(url, user, password, driver)
    
    def query= 'INSERT INTO your-table-name (your-first-column, your-second-column) VALUES (?,?)'
    def params = ['your-first-value', 'your-second-value']
    sql.executeInsert query, params
    
    sql.close()
  6. Change your-database-host, your-database-port, etc. to the real IP address, port, credentials, table name, column name, etc.

  7. Enjoy it

More information:


Side note: I believe it would be faster and easier to use the JDBC Requests sampler

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template