简体   繁体   中英

How to use ResultSet returned by java method in SOAPUI with groovy script?

We have created jar file for a java method and imported it in SOAPUI . We are able to call method, however not able to retrieve query result returned in ResultSet by java method in groovy script def dataRow = GetData.GetRecords(preQuery) . I am new to groovy script. Below is method we have written in java and created jar for it.

package getRecords;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class GetData {

    protected static Connection con = null;
    protected static Statement stmt = null;
    protected static ResultSet result = null;

    //Opening DB connection
    public static void OpenDBConnection(String dbUrl, String driver, String username, String password){

        //Making connection to DB
        try {
            Class.forName(driver);

            con = DriverManager.getConnection(dbUrl, username, password);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //Closing DB connection
    public static void CloseDBConnection(){
        try {
            //Closing DB connection
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    //Executing query and fetching data from DB
    public static ResultSet GetRecords(String query){

        //Executing query and saving result into result set
        try {
            stmt = con.createStatement();

            result = stmt.executeQuery(query);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    public static void main(String args[]){
        System.out.println("DBConnection..");

        GetData gd = new GetData();
        GetData.OpenDBConnection("jdbc:oracle:thin:@test:1530/test", "oracle.jdbc.driver.OracleDriver", "******", "******");
        System.out.println("DB");
    }
}

I suspect your result set is being closed when you return from GetRecords (tip: use camel case for Java method names, starting with a lower-case character) and you may also be jumping JVMs. See also Is it Ok to Pass ResultSet? .

You probably don't need to use your result set as a result set back in soapUI, you just want the data, so a better option would be to populate a bean and return a List of those instead:

public static List<MyBean> GetRecords(String query){

    List<MyBean> myBeans = new ArrayList<>();
    //Executing query and saving result into result set
    try {
        stmt = con.createStatement();

        result = stmt.executeQuery(query);
        while (result.next()) {

            MyBean myBean = new MyBean();
            // Populate the bean...

            myBeans.add(myBean);
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return myBeans;
}

You might also want to investigate the try-with-resources feature that came out with Java 7: it'll handle the closing of your connections automatically.

In SoapUi you can directly do the JDBC call using Groovy scripting.

If you wanted to do some Database operation in soapUI you can write the code in groovy using the corresponding database driver ( DB2, Oracle, Mysql), Until or unless any specific reason to use jar file as you have mentioned.

Making the database connection you need to download and place the jar files inside ( SoapUi install folder/bin/ext

eg.. for Oracle (ojdbc6.jar, orai18n.jar)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM