# Data base get values

**URL:** <https://forum.katalon.com/t/data-base-get-values/33995>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [October 1, 2019, 8:50am UTC](https://forum.katalon.com/t/data-base-get-values/33995 "2019-10-01T08:50:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arturs.pumpa](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/arturs.pumpa/32/16103_2.png) [@arturs.pumpa](https://forum.katalon.com/u/arturs.pumpa)\
**Post date:** [October 1, 2019, 8:50am UTC](https://forum.katalon.com/t/data-base-get-values/33995/1 "2019-10-01T08:50:24Z")

</div>

Hello colleagues!  
Have one question, i am trying to get the following data from DB:  
 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/3/31ae46aa41430cfb1f0d3c5ddd92c48c6cec69d2.png)

i have the keyword for executing the query from database:

```auto
	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](https://europe1.discourse-cdn.com/katalon/original/2X/7/73bf1e6df7af9ac3e9c5223908f68003ff17ed42.png)  
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":

```auto
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'()

```

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [October 1, 2019, 9:12am UTC](https://forum.katalon.com/t/data-base-get-values/33995/2 "2019-10-01T09:12:30Z")

</div>

> [@arturs.pumpa](#):
>
> ```auto
> //Save the result as a string 
> String valueFromDatabase = rs.getString(1) 
> String valueFromDatabase2 = rs.getString(2)
> 
> ```

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:

```auto
	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]

```

---

<div class="post-metadata">

**Author:** ![arturs.pumpa](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/arturs.pumpa/32/16103_2.png) [@arturs.pumpa](https://forum.katalon.com/u/arturs.pumpa)\
**Post date:** [October 1, 2019, 9:45am UTC](https://forum.katalon.com/t/data-base-get-values/33995/3 "2019-10-01T09:45:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arturs.pumpa](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/arturs.pumpa/32/16103_2.png) [@arturs.pumpa](https://forum.katalon.com/u/arturs.pumpa)\
**Post date:** [October 1, 2019, 9:51am UTC](https://forum.katalon.com/t/data-base-get-values/33995/4 "2019-10-01T09:51:55Z")

</div>

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

```auto
[DOEQXNKTHTKH, JOHNQXNKTHTKH]

```

i need get result like this

```auto
DOEQXNKTHTKH
JOHNQXNKTHTKH

```

maybe some how i can get it

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [October 1, 2019, 9:55am UTC](https://forum.katalon.com/t/data-base-get-values/33995/5 "2019-10-01T09:55:22Z")

</div>

> [@arturs.pumpa](#):
>
> [DOEQXNKTHTKH, JOHNQXNKTHTKH]

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

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

```

Try to see if it works,

---

<div class="post-metadata">

**Author:** ![arturs.pumpa](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/arturs.pumpa/32/16103_2.png) [@arturs.pumpa](https://forum.katalon.com/u/arturs.pumpa)\
**Post date:** [October 1, 2019, 9:59am UTC](https://forum.katalon.com/t/data-base-get-values/33995/6 "2019-10-01T09:59:14Z")

</div>

> [@ThanhTo](#):
>
> def valueFromYourKw = CustomKeywords.`yourpathtokeywordpackage.keywordClass.executeQuery\_2\_result’(queryString); println valueFromYourKw[1]; println valueFromYourKw[2];

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

```auto
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

---

<div class="post-metadata">

**Author:** ![arturs.pumpa](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/arturs.pumpa/32/16103_2.png) [@arturs.pumpa](https://forum.katalon.com/u/arturs.pumpa)\
**Post date:** [October 1, 2019, 10:19am UTC](https://forum.katalon.com/t/data-base-get-values/33995/7 "2019-10-01T10:19:14Z")

</div>

i have got the solution =) pretty simple 🙂

```auto
	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

---

<div class="post-metadata">

**Author:** ![ross](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/ross/32/16103_2.png) [@ross](https://forum.katalon.com/u/ross)\
**Post date:** [August 17, 2021, 7:47pm UTC](https://forum.katalon.com/t/data-base-get-values/33995/8 "2021-08-17T19:47:36Z")

</div>

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

Kind regards  
Ross
