Forum Discussion

eass_vijetha's avatar
eass_vijetha
New Contributor
8 years ago
Solved

database connection through groovy

hi,
can any one let me know, how to connect to database through groovy, presently i am using INTER BASE. i want to connect to inter base Data Base through INTER BASE server.while i am trying to connect i got an error
here is my code and error

i request you to please help me on this.

import groovy.sql.Sql

try{
def dbURL="jdbc:interbase://localhost:3050/c:/GDBfile"
def dbUsername="SYSDBA"
def dbPassword="masterkey"
def dbDriver="interbase.interclient.Driver"
def db = Sql.newInstance(dbURL,dbUsername,dbPassword,dbDriver)

// connec
/**********************Select query*******************************/
def q1 = "select count(RES_ID),max(RES_ID) from reservering"

// interact with DB

db.eachRow(q1){
log.info "${it.RES_ID}"
}

}catch(InstantiationException e){
log.info "Some db error"
log.info e.getMessage()

}finally{

// close database connection
db.close()
}

while executing this i got an error like...

is this scenario is correct?

  • Hi,

     

    I think your problem there is just of variable scope i.e. define your variables before the try block:

     

    import groovy.sql.Sql
    def dbURL="jdbc:interbase://localhost:3050/c:/GDBfile"
    def dbUsername="SYSDBA"
    def dbPassword="masterkey"
    def dbDriver="interbase.interclient.Driver"
    def db = Sql.newInstance(dbURL,dbUsername,dbPassword,dbDriver)
    // connec
    /**********************Select query*******************************/
    def q1 = "select count(RES_ID),max(RES_ID) from reservering"
    // interact with DB
    try{
    
    
    db.eachRow(q1){
    log.info "${it.RES_ID}"
    }
    }catch(InstantiationException e){
    log.info "Some db error"
    log.info e.getMessage()
    }finally{
    // close database connection
    db.close()
    }

    Regards,

    Rup