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. jenkins配置email

    # 系统设置 # Jenkins Location # 邮件通知 # 高级 # Failed to send out e-mail 勾选“使用SSL协议” SMTP端口改为465 密码使用授权码,不能 ...

  2. 7-19 PAT Judge(25 分)

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  3. ZedGraph右键菜单怎样禁止它弹出(转)

    private void ZGC_ContextMenuBuilder( ZedGraphControl sender,                     ContextMenuStrip me ...

  4. BZOJ1252:序列终结者

    浅谈\(splay\):https://www.cnblogs.com/AKMer/p/9979592.html 浅谈\(fhq\)_\(treap\):https://www.cnblogs.com ...

  5. PHP代码实现 1

    $PHP-SRC/run-test.php 因为如果在同一个进程中执行, 测试就会停止,后面的测试也将无法执行,php中有很多将脚本隔离的方法比如: system(),exec()等函数,这样可以使用 ...

  6. 黄聪:360浏览器、chrome开发扩展插件教程(1)开发Chrome Extenstion其实很简单

    转载:http://www.cnblogs.com/walkingp/archive/2011/03/31/2001628.html Chrome的更新速度可以说前无古人,现在我每天开机的第一件事就是 ...

  7. WinForm中Application.Idle事件用法

    Application.Idle 事件 描述:当应用程序完成处理并即将进入空闲状态时发生.如果您有必须执行的任务在线程变为空闲之前,请将它们附加到此事件. public partial class F ...

  8. bin sh git@github.com no such file or directory

    window下使用git: 输入命令:git clone git @github.com:Alan0521/dotvim.git 出现/bin/sh:git@github.com no such fi ...

  9. Spring MVC的困惑url-pattern /和/*的区别

    今天在写项目时发现一个spring 总是报org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: No mapping ...

  10. gSOAP:C++编写服务器端

    1.编写头文件cal.h: //gsoap ns service name: calc //gsoap ns service style: rpc //gsoap ns service encodin ...