Forum Discussion

Oskar's avatar
Oskar
New Contributor
9 years ago

How to use a preconfigured Database connection in Groovy

Hi, I created an environment with a database connection and would like to use it also in a groovy script. Has anybody a code example for this?   Cheers, Oskar
  • Oskar's avatar
    Oskar
    9 years ago

    I used environment parameters to do this in SOAP UI NG

     

    import groovy.sql.Sql
    import java.sql.SQLException

    try {

     

    def testDbUser = testRunner.testCase.testSuite.project.getPropertyValue("dbusername")
    def testDbPass = testRunner.testCase.testSuite.project.getPropertyValue("dbpassword")
    def testDbHost = testRunner.testCase.testSuite.project.getPropertyValue("dbhost")
    def testDbPort = testRunner.testCase.testSuite.project.getPropertyValue("dbport")
    def testDbServ = testRunner.testCase.testSuite.project.getPropertyValue("dbservicename")

    def db = [url:'jdbc:oracle:thin:@'+testDbHost+':'+testDbPort+'/'+testDbServ, user:testDbUser, password:testDbPass, driver:'oracle.jdbc.driver.OracleDriver']
    def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
    log.info("using connection " + testDbUser + " " + db.url)

    sql.execute ("some sql")
    sql.execute ("commit")
    log.info("mandate data removed...")
    }
    catch (SQLException e){
    def tmp = e.getSQLState ()
    log.error (tmp, e)
    }