'How to connect the my SQL db and execute the queries and store to dbtable by using jsr223 sampler which code are written in this sampler? Sample code

Please give step by step process

How to connect MySQL db and excute queries and stored those results in db table by using jsr223 sampler? Please give sample code this topic



Solution 1:[1]

  1. Download MySQL JDBC Driver and drop it to "lib" folder of your JMeter installation (or other folder in JMeter Classpath)

  2. Restart JMeter to pick up the .jar

  3. Add Thread Group to your Test Plan

  4. Add JSR223 Sampler to your Thread Group

  5. Put the following code into "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 real IP address, port, credentials, table name, column name, etc.

  7. Enjoy.

More information:


P.S. I believe using JDBC Request sampler would be way faster and easier

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T