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)
    }

  • kondasamy's avatar
    kondasamy
    Regular Contributor

    I'm not sure whether you can reuse the database connection that you set-up in JDBC test step through Groovy! And BTW, JDBC connection manager is available only in Pro/ NG version. Digging the SoapUI Pro's Javadoc might give a clue. As of now, I would recommend you to establish a fresh connection to the database.

     

    I hope this link would help you to proceed further - http://groovy-lang.org/databases.html

     

    Thanks,

    Kondasamy

    • Oskar's avatar
      Oskar
      New Contributor

      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)
      }