title: Creating Jmeter test of jdbc using java code
body: <p>I want to create a JMeter test of JDBC using java code. I have tried the following code. It's working fine for me giving me status: 200 but the values are not being stored in the MySQL database. Can anyone please help me in finding the error in this code or provide some appropriate solution to it.</p>
  <pre><code> public class Jdbc_Sampler {
  
  public static void main(String[] argv) throws Exception {
      
      final String jmeterHome = &quot;C:\\apache-jmeter-5.3&quot;;
  
      StandardJMeterEngine jmeter = new StandardJMeterEngine();
      JMeterUtils.setJMeterHome(jmeterHome);
      JMeterUtils.loadJMeterProperties(jmeterHome + System.getProperty(&quot;file.separator&quot;) + &quot;bin&quot; + System.getProperty(&quot;file.separator&quot;) + &quot;jmeter.properties&quot;);
  
      HashTree testPlanTree = new HashTree();
  
      DataSourceElement jdbcDataSource = new DataSourceElement();
      jdbcDataSource.setName(&quot;JDBC Connection Configuration&quot;);
      jdbcDataSource.setProperty(&quot;dataSource&quot;, &quot;variable_name&quot;);
      jdbcDataSource.setProperty(&quot;dbUrl&quot;, &quot;jdbc:mysql://localhost:3306/mysqldb?useSSL=false&quot;);
      jdbcDataSource.setProperty(&quot;driver&quot;, &quot;com.mysql.cj.jdbc.Driver&quot;);
      jdbcDataSource.setProperty(&quot;username&quot;, &quot;root&quot;);
      jdbcDataSource.setProperty(&quot;password&quot;, &quot;root&quot;);
      jdbcDataSource.setProperty(&quot;poolMax&quot;, &quot;0&quot;);
      jdbcDataSource.setProperty(&quot;connectionAge&quot;, &quot;5000&quot;);
      jdbcDataSource.setProperty(&quot;timeout&quot;, &quot;10000&quot;);
      jdbcDataSource.setProperty(&quot;trimInterval&quot;, &quot;60000&quot;);
      jdbcDataSource.setProperty(TestElement.TEST_CLASS, DataSourceElement.class.getName());
      jdbcDataSource.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
  
      JDBCSampler jdbcSampler = new JDBCSampler();
      jdbcSampler.setName(&quot;JDBC Request&quot;);
      jdbcSampler.setProperty(&quot;dataSource&quot;, &quot;variable_name&quot;);
      jdbcSampler.setProperty(&quot;queryType&quot;, &quot;Update Statement&quot;);
      jdbcSampler.setProperty(&quot;query&quot;, &quot;Insert into tablenm values(7,'ghi',10);&quot;);
      jdbcSampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
      jdbcSampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
  
      LoopController loopController = new LoopController();
      loopController.setLoops(1);
      loopController.setFirst(true);
      loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
      loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
      loopController.initialize();
  
      ThreadGroup threadGroup = new ThreadGroup();
      threadGroup.setName(&quot;Example Thread Group&quot;);
      threadGroup.setNumThreads(1);
      threadGroup.setRampUp(1);
      threadGroup.setSamplerController(loopController);
      threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
      threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
  
      TestPlan testPlan = new TestPlan(&quot;JMeter JDBC Test Plan&quot;);
      testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
      testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
      testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
  
      testPlanTree.add(testPlan);
      HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
      threadGroupHashTree.add(jdbcDataSource);
      threadGroupHashTree.add(jdbcSampler);
  
      SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + System.getProperty(&quot;file.separator&quot;) + &quot;jdbc.jmx&quot;));
  
  
      Summariser summer = null;
      String summariserName = JMeterUtils.getPropDefault(&quot;summariser.name&quot;, &quot;summary&quot;);
      if (summariserName.length() &gt; 0) {
          summer = new Summariser(summariserName);
      }
  
      String logFile = jmeterHome + System.getProperty(&quot;file.separator&quot;) + &quot;jdbc.jtl&quot;;
      ResultCollector logger = new ResultCollector(summer);
      logger.setFilename(logFile);
      testPlanTree.add(testPlanTree.getArray()[0], logger);
  
      jmeter.configure(testPlanTree);
      jmeter.run();
  
      System.exit(0);
  
  
    }
   }
  </code></pre>