try, catch and finally in db connection
Forming groovy connection string and obtaining Connection Object
Firing Select Query and obtaining results
Foreach and rows functions
Finding number of rows in result
import groovy.sql.Sql// obtain the connection to database// do the transaction// close database connectiontry{// connecting to dbdef dbURL="jdbc:mysql://localhost:3306/retail"def dbUsername="root"def dbPassword=""def dbDriver="com.mysql.jdbc.Driver"def db = Sql.newInstance(dbURL,dbUsername,dbPassword,dbDriver)// interact with DB/**********************Select query*******************************/def q1 = "select * from product" // simple select query - more than 1 rowdef q2 = "select * from product where prod_id='4'" // 1 rowdef q3 = "select * from product where prod_name like '%QTP%'" // more than 1 // eachRow, rowsdb.eachRow(q3){//log.info "${it.prod_name}" +" -- " + "${it.prod_price}" log.info it[0] + " " + it[1] + " " + it[2]}// count of the rows which i get// add variables in the querydef x ='Nike'def q4 = "select * from product where prod_name=$x"db.eachRow(q4){//log.info "${it.prod_name}" +" -- " + "${it.prod_price}" log.info it[0] + " " + it[1] + " " + it[2]}log.info "*******Multiple parameters**********"def name='Catch 22'def category_id='6'def pro_id='12'def q5 = "select * from product where prod_name=$name and cat_id=$category_id and prod_id=$pro_id"db.eachRow(q5){//log.info "${it.prod_name}" +" -- " + "${it.prod_price}" log.info it[0] + " " + it[1] + " " + it[2]}log.info "Using list in the query"def params=['Catch 22','6','12']def q6 = "select * from product where prod_name=? and cat_id=? and prod_id=?"db.eachRow(q6,params){ log.info "$it.prod_name"}log.info "****************ROWS Function***********************"def result = db.rows(q1)log.info "Total number of rows in the result " + result.size()log.info result.get(0).get("prod_id")+" "+result.get(0).get("prod_name")log.info result.get(5).get("prod_id")+" "+result.get(5).get("prod_name")// complete outputfor(i=0;i