How to print Resultset using for loop

Hey guys,

I got list of records using sql query and store it in ResultSet in katalon.
now i want to print column value using for loop

UserName : Mihir
UserName : Kadiya
UserName : TEst

i have do some code like this way

CustomKeywords.'com.database.dbcontext.connectDB'(GlobalVariable.host, GlobalVariable.dbName, 
GlobalVariable.db_userName, GlobalVariable.db_password)
def productQuery = "Select * from Users;"
ResultSet productResult = CustomKeywords.'com.database.dbcontext.executeQuery'(productQuery)for (var in productResult) {      System.out.println(var.getString("UserName"))}

Hi Mihir,

how do you get your ResultSet? I am afraid you cannot use foreach loop for this type. This is how I get data from SQL server and it looks like easier solution for me.

import com.kms.katalon.core.util.KeywordUtilimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport groovy.sql.Sqlpublic class SQLHandler {		public SQLHandler(SqlServerData sqlData) {		this.sqlData = sqlData	}		SqlServerData sqlData	String dbConnString = sqlData.getdbConnString()	String dbUsername = sqlData.getDbUsername()	String dbPassword = sqlData.getDbPassword()	String dbDriver = sqlData.getDbDriver()				Sql sql = Sql.newInstance(dbConnString,dbUsername,dbPassword,dbDriver)						public List getSelectResults(String query) {		KeywordUtil.logInfo("Executing the query: " + query)		List results = sql.rows(query)		if(results.size() == 0){		    KeywordUtil.markFailedAndStop("No rows returned from SQL Server")		}		return sql.rows(query)	}}

SQLHandler._getSelectResults() _returns a list of result rows. Each result row contains columnName:columnValue pair, co you can easily get any value from any row. This may be a sample result of my method:

[Id:1, UserName:John], [Id:2, UserName:Doe]

You can use foreach here without any problem.

List resultSet = sql.getSelectResults("select * from User")for(def res in resultSet) {	println res.get("UserName")}

Hi Marek Melocik
can you please let me know where i create SQLHandler class
because getting error in SQLHandler constructor method

Oh, I forgot to mention, I use another class called SqlServerData for storing connection information. You don’t have to create this class, just change a constructor and pass all required Strings in it.

public SQLHandler(String dbConnString, String dbUsername, String dbPassword, String dbDriver) {        ...}

okay, thanks! Marek Melocik

Marek Melocik can you please provide SqlServerData class with scrumble data which you have created…

This is what I have:

public class SqlServerData {	String dbConnString="jdbc:sqlserver://sqlServerHostname:port;databaseName=dbName"	String dbUsername="username"	String dbPassword="password"	String dbDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"// plus all getters and setters here}