Home  >  Q&A  >  body text

Learn how to use the JSR223 sampler to store SQL query results into a database table

<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粉633309801P粉633309801417 days ago431

reply all(1)I'll reply

  • P粉302484366

    P粉3024843662023-08-30 20:29:00

    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

    reply
    0
  • Cancelreply