Search code examples
javajmeterbeanshelljsr223

Execute simple java file using jmeter


public static void main(String[] args) {
    String filename ="test.csv";
    try {
        FileWriter fw = new FileWriter(filename);
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://server;databaseName;","user","password");
        String query = "select * from table";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            fw.append(rs.getString(1));
            fw.append(',');
            fw.append(rs.getString(2));
            fw.append(',');
            fw.append(rs.getString(3));
            fw.append('\n');
           }
        fw.flush();
        fw.close();
        conn.close();
        System.out.println("CSV File is created successfully.");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I just want to execute this java file in jmeter using BeanShell Sample or JSR223 Timer. Can anyone please help for this?


Solution

  • You don't need a file for that.

    In your JSR223 Timer, select Groovy as language and put your code in it without the main function signature just:

    System.out.println("Hello");

    Ensure you check 'Cache ...'