protea protea - 23 days ago 11
Groovy Question

Getting no response when I run the following database connection script from Groovy[Soapui]

I am trying to run this script from Groovy[soapUI] but i am not getting errors and not the sql command is not returning any results. am I missing anything crucial here?

import groovy.sql.Sql
import java.sql.*
import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session


// ssh login
String sshHost = 'test.com'
String sshUser = 'test'
String sshPass = 'test'
int sshPort = 22

// database login
targetHost = 'localhost'
targetUser = 'test'
targetPass = 'test'
targetPort = 3306
lport = 4328

JSch jsch = new JSch();
Session session = jsch.getSession(sshUser, sshHost, sshPort);
session.setPassword(sshPass);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
int assinged_port=session.setPortForwardingL(lport, targetHost, targetPort);

Connection con = null;
String driver = "org.mariadb.jdbc.Driver";
String connectionString = "jdbc:mariadb://" + targetHost +":" + lport + "/";
con = DriverManager.getConnection(connectionString, targetUser, targetPass);
Statement st = con.createStatement();
String sql = "select * from SS_System.tblcompanies where companyid=495555"
st.executeQuery(sql);
st.close()
session.disconnect()


Also, after adding bunch of log.info statements I am getting the following response:

Sun Nov 13 21:39:30 EST 2016:INFO:com.jcraft.jsch.Session@4e6b3063
Sun Nov 13 21:39:31 EST 2016:INFO:null
Sun Nov 13 21:39:31 EST 2016:INFO:4336
Sun Nov 13 21:39:31 EST 2016:INFO:jdbc:mysql://localhost:4336/
Sun Nov 13 21:39:31 EST 2016:INFO:org.mariadb.jdbc.MariaDbConnection@14f67389
Sun Nov 13 21:39:31 EST 2016:INFO:org.mariadb.jdbc.MariaDbStatement@401b321f
Sun Nov 13 21:39:31 EST 2016:INFO:org.mariadb.jdbc.internal.queryresults.resultset.MariaSelectResultSet@74b9f5af

Answer

Perhaps you should do something with the query result

// instead of this 
st.executeQuery(sql)

// do something like
java.sql.ResultSet rs = st.executeQuery(query);

and then iterate results as described here https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html#processing_resultset_objects

while (rs.next()) {
    String value = rs.getString("COLUMN_NAME");
    log.info("COLUMN_NAME:"+value)
}