How would I execute this Java code from Katalon

Katalon Studio Version:
KSE 8.4.0, Build 208
Windows 10 Enterprise (64-bit)
Chrome: 107.0.5304.62 (Official Build) (32-bit)

Hello folks,
How would I execute this java code from with Katalon?

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Scratch {
    public static void main(String[] args) throws SQLException {
        Connection oracle = DriverManager.getConnection("jdbc:oracle:thin:@//d-xxx-xxx.xx.net:1521/TEST1.xg.net","","");
        Statement statement1 = oracle.createStatement();
        ResultSet rs1 = statement1.executeQuery("SELECT pa.PROSPECT_APPLICATION_ID \n" +
                " TEST NOTES
                " TEST NOTES
                " TEST NOTES
        List<String> prospectApplicationsToDeactivate = new ArrayList<>();
        while(rs1.next()) {
            prospectApplicationsToDeactivate.add(rs1.getString("PROSPECT_APPLICATION_ID"));
        }
        System.out.println(String.join(",", prospectApplicationsToDeactivate));
        System.out.println(prospectApplicationsToDeactivate.size() + " Records");
        System.out.println("Should I deactivate these Prospect Applications? Y for Yes . Any other key for no.");
        Scanner scanner = new Scanner(System.in);
        String answer = scanner.nextLine();
        if (answer.equals("Y")){
            for (String prospectApplicationId : prospectApplicationsToDeactivate){
                Statement statement = oracle.createStatement();
                statement.executeQuery("UPDATE PROSPECT_APPLICATIONS pa SET pa.INACTIVE_DATE = trunc(SYSDATE) WHERE pa.PROSPECT_APPLICATION_ID="+prospectApplicationId);
            }
            System.out.println("Done!");
        }
        else {
            System.out.println("Exited!");
        }
    }
}
1 Like

You can create a Java code in <projectDir>/Include/script/groovy.

The Groovy compiler will happily compile a *.java file in this directory.


In a Test Case script, you want

import Scratch

Scratch.main()

This test case should work.

1 Like

wrong:

class Scratch {

right:

public class Scratch {
2 Likes

Thanks @kazurayam!