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 connection

try{
// connecting to db
def 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 row
def q2 = "select * from product where prod_id='4'" // 1 row
def q3 = "select * from product where prod_name like '%QTP%'" // more than 1 // eachRow, rows db.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 query
def 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 output
for(i=0;i<result.size();i++){
log.info result.get(i).get("prod_id")+" "+result.get(i).get("prod_name")
}
// adding parameters
log.info "Adding parameters in the query with variable"
result = db.rows(q4)
log.info "Total number of rows " + result.size()
log.info result.get(0).get("prod_id") + " " + result.get(0).get("prod_name") // Map containing the parameters
log.info "*******MAP********"
def myMap =[x:'Harry Potter',y:'11']
def query="select * from product where prod_id=:y and prod_name=:x"
result = db.rows(query,myMap)
log.info "Total rows " + result.size
log.info result.get(0).get("prod_id") + " " + result.get(0).get("cat_id")+" "+result.get(0).get("prod_name") // List containing the parameters
log.info "********LIST************"
def p1=['Catch 22','6','12']
def q7 = "select * from product where prod_name=? and cat_id=? and prod_id=?"
result = db.rows(q7,p1)
log.info "Total rows " + result.size
log.info result.get(0).get("prod_id") + " " + result.get(0).get("cat_id")+" "+result.get(0).get("prod_name") // firing a query
}catch(Exception e){
log.info "Some db error"
log.info e.getMessage() }finally{ // close database connection
db.close()
}

[Training Video - 7] [Database connection] Part 1的更多相关文章

  1. [Training Video - 7] [Database connection] Various databases which are supported, Drivers for database connection, SQL Groovy API

    Various databases which are supported Drivers for database connection groovy.sql.Sql package SoapUI怎 ...

  2. Only one database connection at a time is supported

      Only one database connection at a time is supported 在网上找到了2个方法: 1. VSS在使用过程中,尤其是数据迁移的过程中,可能会出现上述情况 ...

  3. Database Connection Pool Library | Libzdb

    Database Connection Pool Library | Libzdb A small, easy to use Open Source Database Connection Pool ...

  4. 解决:wordpress error establishing a database connection problem

    我是个网站菜鸟,刚开始搭建LAMP环境的时候,就要了我半条老命. 没办法,懂的东西太少,LAMP是什么我都不懂,域名是什么,我也被不懂,为什么想要有个网站就要有服务器我还是不懂.一步步地自己去钻,去看 ...

  5. talend openstudio 在OracleInput组件中guess Schema 出现Database connection is failed 的错误

    错误描述: talend openstudio 在OracleInput组件中guess Schema 出现Database connection is failed 的错误. 查看错误详情,发现错误 ...

  6. ArcCatalog连接ArcSDE连接报:unable to create new database connection file,permission is denied

    参考博文:链接 ArcCatalog连接ArcSDE连接报:unable to create new database connection file,permission is denied 最近经 ...

  7. django:MySQL Strict Mode is not set for database connection 'default'

    ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'        HINT: MySQL's ...

  8. [转]Setting the NLog database connection string in the ASP.NET Core appsettings.json

    本文转自:https://damienbod.com/2016/09/22/setting-the-nlog-database-connection-string-in-the-asp-net-cor ...

  9. Laradock Laravel database connection refused

    Laradock Laravel database connection refused SHARE  Laradock is a PHP development environment which ...

随机推荐

  1. [CF995F]Cowmpany Cowmpensation

    codeforces description 一棵\(n\)个节点的树,给每个节点标一个\([1,m]\)之间的编号,要求儿子的权值不大于父亲权值.求方案数.\(n\le3000,n\le10^9\) ...

  2. 集合(List、Set、Map)

    List,Set是继承自Collection接口,Map不是 public interface List<E> extends Collection<E> { public i ...

  3. StreamSets Data Collector Edge 说明

    Data Collector Edge 是不包含界面的agent 安装 下载包 https://streamsets.com/opensource tar xf streamsets-datacoll ...

  4. vue项目修改favicon

    首先你的在你的static文件中添加favicon.icon 然后通过以下方式进行修改 1)方式一:修改index.html文件 <link rel="shortcut icon&qu ...

  5. 【android】SDK在线升级

    1.修改本地hosts文件 hosts文件位置:C:\Windows\System32\drivers\etc\hosts 在底部添加:203.208.46.146 dl-ssl.google.com ...

  6. vmware克隆linux网络配置

    一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候会报network is unreachable 并且怎么也找不到问 ...

  7. laravel Hash密码 校对

    laravel加密 是使用hash不可逆的,但是可以对加密后的密码进行校对 $data = $r->all();$id = $data['id'];$user_password = bcrypt ...

  8. Java--不可覆盖的方法

    私有方法不能被覆盖: 因为private被自动认为final,对子类是屏蔽的,那么子类中的相同方法就是一个新的方法,编译器不会报错但也不会按期望运行: public class ClassA { pr ...

  9. Flask 模板语法

    Flask中默认的模板语言是Jinja2 STUDENT = {'name': 'Old', 'age': 38, 'gender': '中'}, STUDENT_LIST = [ {'name': ...

  10. Ubuntu下VIM使用指南

    基本命令: Esc:VIM中的万能功能键之一,基本上任何时候按这个键,都可以返回VIM的普通状态. i:在普通状态下按i可以进入“插入”编辑状态,这个时候按方向键移动光标,在想要输入的地方输入字符,用 ...