Data base get values

Hello colleagues!
Have one question, i am trying to get the following data from DB:
image

i have the keyword for executing the query from database:

	def executeQuery_2_result(String queryString)  {
		Statement stm = connection.createStatement()
		ResultSet rs = stm.executeQuery(queryString)
		//Move the cursor in the ResultSet table to the first row
		rs.next()
		//Save the result as a string
		String valueFromDatabase = rs.getString(1)

		String valueFromDatabase2 = rs.getString(2)
		//Print our result from the database as a string to the console
		println(valueFromDatabase)
		println(valueFromDatabase2)
		return [valueFromDatabase2, valueFromDatabase]

but it dont work it only returns the values if there are in one row like this:
image
could you please help me how can i modify my keyword that he returns the values which i want

in testcase the code is following":

CustomKeywords.'md_db.sql.connectDB'(GlobalVariable.dbname, GlobalVariable.schemename, GlobalVariable.dbport, GlobalVariable.dbuser, GlobalVariable.dbpass)
String loan_amount = CustomKeywords.'md_db.sql.executeQuery_2_rows'('select internal_account from u_transactions where bill_id = '+ invoice_number +'  order by id desc;')
CustomKeywords.'md_db.sql.closeDatabaseConnection'()

I think this piece of code is basically retrieving values from the first and the second column of the current data row. Can you write something like this:

	def executeQuery_2_result(String queryString)  {
		Statement stm = connection.createStatement()
		ResultSet rs = stm.executeQuery(queryString)
		//Move the cursor in the ResultSet table to the first row
		rs.next()
		//Save the result as a string
		String valueFromDatabase = rs.getString(1)
                //Move the cursor in the ResultSet table to the second row
		rs.next()
               // Still get the value from the first column
		String valueFromDatabase2 = rs.getString(1)
		//Print our result from the database as a string to the console
		println(valueFromDatabase)
		println(valueFromDatabase2)
		return [valueFromDatabase2, valueFromDatabase]
1 Like

Yes it is working ))) thank you for your answer

onme more question, maybe you also know the answer
from my keyword it retuns value like:

[DOEQXNKTHTKH, JOHNQXNKTHTKH]

i need get result like this

DOEQXNKTHTKH
JOHNQXNKTHTKH

maybe some how i can get it

Most likely your function (keyword) returns an array. To access each element you can do something like:

def valueFromYourKw = CustomKeywords.`yourpathtokeywordpackage.keywordClass.executeQuery_2_result'(queryString);
println valueFromYourKw[1];
println valueFromYourKw[2];

Try to see if it works,

1 Like

when i try to do like you say it prints the following value

2019-10-01 12:57:29.182 DEBUG testcase.Download_pfd                    - 15: println(loan_amount[1])
1
2019-10-01 12:57:29.185 DEBUG testcase.Download_pfd                    - 16: println(loan_amount[2])
9

like he take one number from sql query result 1920 =) interesting

i have got the solution =) pretty simple :slight_smile:

	def executeQuery_2_rows(String queryString)  {
		Statement stm = connection.createStatement()
		ResultSet rs = stm.executeQuery(queryString)
		//Move the cursor in the ResultSet table to the first row
		rs.next()
		//Save the result as a string
		String valueFromDatabase = rs.getString(1)
		rs.next()
		String valueFromDatabase2 = rs.getString(1)
		//Print our result from the database as a string to the console
		println(valueFromDatabase)
		println(valueFromDatabase2)
		String finalvalue = valueFromDatabase2 +'\n' +valueFromDatabase
		return finalvalue

i got two strings summ and then i return this new string

1 Like

Hi @ThanhTo How would you go about doing this dynamically?
I.e. if the number of “valueFromDatabase” is unknown

Kind regards
Ross