Forum Discussion

ShivaniKamboj's avatar
ShivaniKamboj
Occasional Contributor
7 years ago

DataBase Connection in SetUp script and use the same in Groovy Test Step

Hi  All,

 

I am currently stuck in an issue where i want to configure JDBC connection either in SetUp Script or the one configured at project level 'DataBases' menu and could use the same sql instance in any of the teststep mostly groovy test step while hitting sql query in it.

 

Here i am trying , but not working:

import groovy.sql.Sql
import com.eviware.soapui.support.GroovyUtils

def dbURL="jdbc:jtds:sqlserver://serverNme;databaseName=DBName;trusted_connection=yes "

def dbUsername = ""
def dbPassword = ""
def dbDriver = "net.sourceforge.jtds.jdbc.Driver"
def sql = Sql.newInstance(dbURL,dbDriver)
context.setProperty("DBCon", sql)

 

Using this sql instance in my teststep(Groovy) as:

import groovy.sql.Sql
import com.eviware.soapui.support.GroovyUtils

def sqlObj=context.getProperty("DBCon")
def connection = sqlObj.firstRow("SELECT * FROM Table ")

log.info connection

 

 

Thanks!!

~Shivani

 

  • jhanzeb1's avatar
    jhanzeb1
    Frequent Contributor

    Hi Shivani,

     

    Are you currently able to create a SQL connection at all? 

     

     

    • ShivaniKamboj's avatar
      ShivaniKamboj
      Occasional Contributor

      Yes I am able to connect via  SQLConnection.

       

      But the situation is i have written groovy code for connection as well as query in same groovy teststep.

      And, there is option in Ready APi to make or configure JDBC connection at project level using Databses menu[Refer to attachment]. I want ot use either this configuration in my grrovy script or sql connection made in setup script.

       

      ~Shivani

       

      • jhanzeb1's avatar
        jhanzeb1
        Frequent Contributor

        There are two options. 

         

        First option - Create a JDBC connection via "databases" and you can build your queries i.e select X from Z where A = 2 etc. Once you are happy with the query, the outcome gets stored in the custom properties automatically and to call that custom property all you have to do is ${DataSourceName#PropertyName}. This is simple and straight forward way for data-driving. 

        Please find link below to help you.

        https://support.smartbear.com/readyapi/docs/testing/data-driven/concepts.html

         

        Second option - Create a JDBC connection via groovy script and store that connection in a custom property and just recall that custom property everytime you want to run the sql connection. You will then write your own custom query and store the query output manually into custom properties.

         

        I personally prefer option one simply because it's easy and less time consuming, though if you have some very complex custom queries that "query builder" cannot support then you could use the second option I suppose.