[Training Video - 7] [Database connection] Part 1
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的更多相关文章
- [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怎 ...
- Only one database connection at a time is supported
Only one database connection at a time is supported 在网上找到了2个方法: 1. VSS在使用过程中,尤其是数据迁移的过程中,可能会出现上述情况 ...
- Database Connection Pool Library | Libzdb
Database Connection Pool Library | Libzdb A small, easy to use Open Source Database Connection Pool ...
- 解决:wordpress error establishing a database connection problem
我是个网站菜鸟,刚开始搭建LAMP环境的时候,就要了我半条老命. 没办法,懂的东西太少,LAMP是什么我都不懂,域名是什么,我也被不懂,为什么想要有个网站就要有服务器我还是不懂.一步步地自己去钻,去看 ...
- talend openstudio 在OracleInput组件中guess Schema 出现Database connection is failed 的错误
错误描述: talend openstudio 在OracleInput组件中guess Schema 出现Database connection is failed 的错误. 查看错误详情,发现错误 ...
- ArcCatalog连接ArcSDE连接报:unable to create new database connection file,permission is denied
参考博文:链接 ArcCatalog连接ArcSDE连接报:unable to create new database connection file,permission is denied 最近经 ...
- 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 ...
- [转]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 ...
- Laradock Laravel database connection refused
Laradock Laravel database connection refused SHARE Laradock is a PHP development environment which ...
随机推荐
- element-ui打包和运行报错处理
最近在使用webpack打包过程中遇到element-ui报错. 说明一下情况:之前在原有的配置文件和node_modules依赖包下打包的过程中,一直都很顺利,但是突然就报错了,很尴尬啊! 1 ER ...
- python中django框架的csrf验证
在form表单以post的方式提交时,django默认会带一个验证的机制csrf验证 <form action="/day02/login/" method="po ...
- yarn workspaces基本试用
初始化项目 yarn init -y 添加workspaces 支持 修改package.json { "name": "second", "vers ...
- memsql filesystem pipeline 试用
一些功能类似drill ,比如s3,file ... 创建file pipeline 准备file mkdir -p /opt/db/ touch books.txt 内容如下: The Catche ...
- Nginx 服务器伪静态配置不当造成 Access denied
Nginx 服务器伪静态配置不当造成 Access denied 有群有反馈将 FastAdmin 布署到阿里云后无法打开后台. 出现如下提示,首页是可以打开,点登录链接后出现的.(下是群友的截图) ...
- hdu 4336 Card Collector——最值反演
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 点集中最早出现的元素的期望是 min ,最晚出现的元素的期望是 max :全部出现的期望就是最晚出现 ...
- LCD RGB 控制技术 时钟篇(下)
我们先回顾一下之前的典型时序图 在这个典型的时序图里面,除了上篇博文讲述的HSYNC VSYNC VDEN VCLK这几信号外,我们还能看见诸如HSPW. VSPW,HBPD. HFPD,VBPD. ...
- python 书籍推荐 一
最近"瑞丽模特学Python"的热点牵动了大江南北程序员的心,有人说这是炒作,也有人说这是推广Python的爆点...我嘿嘿一笑,美女就是美女,眼光那是杠杠的,不仅人美,学的语言也 ...
- GCC参数详解 二
1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Compilation) 2.3汇编(Assembly) 2.4连接(Linking) 3多个程序文件的编译 4检错 5库文件连接 5.1编译成 ...
- 黄聪:国内com域名转移到Godaddy详细教程(转)
原文:http://www.cnblogs.com/hsapphire/archive/2010/01/16/1649743.html 最近CCTV进行大量报道色情新闻,还举报CNNIC监管CN域名不 ...