Forum Discussion

NewWonder's avatar
NewWonder
New Contributor
14 years ago

setup script for a database connection

Hi,

I am new to soapUI 4.0.1 and wanted to setup a database connection using the setup script. However I am failing to do so .Here is my code i am using , can anybody help?

The connection string , user id and password are correct.

import groovy.sql.Sql

log.info "Preparing Connection"

try{
sql = SqlnewInstance('jdbc:db2://connection','wonder','welcome9','com.ibm.db2.jcc.DB2Driver')
}
catch (Exception e)
{
log.error "Could not establish connection to the database."
}

4 Replies

  • dva1946's avatar
    dva1946
    Occasional Contributor
    Solution is fairly simple, if you have the right pieces (based on what I have gathered / tested / used recently):

    Below is what we are using for a DB connection to Oracle.
    You can edit for your DB type.
    Make sure you also have JDBC connection properly configure.
    Note we define our DB connection properties at the TESTSUITE level.

    Groovy Script
    -------------
    // Database connection:
    // All these properties are defined at the TEST SUITE LEVEL
    // Properties used:
    // DenServer25
    // Password
    // sid
    // Database
    // DBport

    // Extract global property value as local Variables
    def scriptDenServer25 = context.expand( '${#TestSuite#DenServer25}' )
    def scriptPassword = context.expand( '${#TestSuite#Password}' )
    def scriptSid = context.expand( '${#TestSuite#sid}' )
    def scriptDatabase = context.expand( '${#TestSuite#Database}' )
    def scriptDBport = context.expand( '${#TestSuite#DBport}' )

    //** DVA 12-28-11 Resolve db connection **//
    import groovy.sql.Sql
    import com.eviware.soapui.support.GroovyUtils

    log.info "Connecting to DB here";

    com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("oracle.jdbc.driver.OracleDriver")
    //***************************************//
    log.info "Running Query for TestCase1 - DBConnection R&D";

    sql = Sql.newInstance("jdbc:oracle:thin:@${scriptDenServer25}:${scriptDBport}:${scriptSid}", "${scriptDatabase}", "${scriptPassword}", "oracle.jdbc.pool.OracleDataSource")

    // **TESTCASE** //
    def scriptMOD = context.expand( '${#TestCase#MOD}' )
    def scriptLIMIT = context.expand( '${#TestCase#LIMIT}' )

    // **MAIN QUERY ** //

    def aa = 0
    def bb = 0
    sql.eachRow("select my_id from mytable where type = ${scriptMOD} and deleted_flag = 0 and ROWNUM <= ${scriptLIMIT} order by my_id") { row ->

    //set property to value from row selected
    testRunner.testCase.setPropertyValue( "dbmyID", "$row.MY_ID" )

    //log.info "Set Property dbofferingID = $row.OFFERING_ID";
    // Now run desired test steps. Must send each individually.
    testRunner.runTestStepByName("Property Transfer to getCatalogOffering");
    testRunner.runTestStepByName("getCatalogOffering");
    bb++
    }
    log.info "Records Processed = $bb";

    sql.close()
  • Aaronliu's avatar
    Aaronliu
    Frequent Contributor
    Great
    dva1946 wrote:
    Solution is fairly simple, if you have the right pieces (based on what I have gathered / tested / used recently):

    Below is what we are using for a DB connection to Oracle.
    You can edit for your DB type.
    Make sure you also have JDBC connection properly configure.
    Note we define our DB connection properties at the TESTSUITE level.

    Groovy Script
    -------------
    // Database connection:
    // All these properties are defined at the TEST SUITE LEVEL
    // Properties used:
    // DenServer25
    // Password
    // sid
    // Database
    // DBport

    // Extract global property value as local Variables
    def scriptDenServer25 = context.expand( '${#TestSuite#DenServer25}' )
    def scriptPassword = context.expand( '${#TestSuite#Password}' )
    def scriptSid = context.expand( '${#TestSuite#sid}' )
    def scriptDatabase = context.expand( '${#TestSuite#Database}' )
    def scriptDBport = context.expand( '${#TestSuite#DBport}' )

    //** DVA 12-28-11 Resolve db connection **//
    import groovy.sql.Sql
    import com.eviware.soapui.support.GroovyUtils

    log.info "Connecting to DB here";

    com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("oracle.jdbc.driver.OracleDriver")
    //***************************************//
    log.info "Running Query for TestCase1 - DBConnection R&D";

    sql = Sql.newInstance("jdbc:oracle:thin:@${scriptDenServer25}:${scriptDBport}:${scriptSid}", "${scriptDatabase}", "${scriptPassword}", "oracle.jdbc.pool.OracleDataSource")

    // **TESTCASE** //
    def scriptMOD = context.expand( '${#TestCase#MOD}' )
    def scriptLIMIT = context.expand( '${#TestCase#LIMIT}' )

    // **MAIN QUERY ** //

    def aa = 0
    def bb = 0
    sql.eachRow("select my_id from mytable where type = ${scriptMOD} and deleted_flag = 0 and ROWNUM <= ${scriptLIMIT} order by my_id") { row ->

    //set property to value from row selected
    testRunner.testCase.setPropertyValue( "dbmyID", "$row.MY_ID" )

    //log.info "Set Property dbofferingID = $row.OFFERING_ID";
    // Now run desired test steps. Must send each individually.
    testRunner.runTestStepByName("Property Transfer to getCatalogOffering");
    testRunner.runTestStepByName("getCatalogOffering");
    bb++
    }
    log.info "Records Processed = $bb";

    sql.close()
  • Aaronliu's avatar
    Aaronliu
    Frequent Contributor
    NewWonder wrote:
    Hi,

    I am new to soapUI 4.0.1 and wanted to setup a database connection using the setup script. However I am failing to do so .Here is my code i am using , can anybody help?

    The connection string , user id and password are correct.

    import groovy.sql.Sql

    log.info "Preparing Connection"

    try{
    sql = SqlnewInstance('jdbc:db2://connection','wonder','welcome9','com.ibm.db2.jcc.DB2Driver')
    }
    catch (Exception e)
    {
    log.error "Could not establish connection to the database."
    }


    here is a sample I used Oracle:

    import groovy.sql.Sql
    def driver="oracle.jdbc.driver.OracleDriver"
    def path="jdbc:oracle:thin:@host:1521/serviceName"
    def username='wonder'
    def password='welcome9'
    def sql = Sql.newInstance(path,username,password,driver)
    def state="select * from dual"
    sql.execute(state)