Forum Discussion

eass_vijetha's avatar
eass_vijetha
New Contributor
9 years ago

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 conn...
  • rupert_anderson's avatar
    rupert_anderson
    9 years ago

    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