[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 ...
随机推荐
- HDU1502 Regular Words DP+大数
要是c语言可以和java一样写大数就好了,或者我会写重载就好了,最后还是只能暴力一把. 开始写的记忆化搜索,然而n=10就超过LL了 #include<cstdio> #include&l ...
- TCP/IP分析
TCP/IP四层模型 TCP/IP参考模型 ISO制定的OSI参考模型的过于庞大.复杂招致了许多批评.与此对照,由技术人员自己开发的TCP/IP协议栈获得了更为广泛的应用.如图2-1所示,是TCP/I ...
- test20181024 zi
题意 分析 这种题一般是推公式,发现必须求得的量,然后定义函数记忆化. 然后那些函数里面又是递归处理,合并. 代码 为了不爆空间,用map存记忆化内容. #include<bits/stdc++ ...
- 【python】Python框架、库和软件资源大全
很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世界各地的程序员们都能够贡献他们的代码与创新. Python就是这样一门受到全世界各地开源社区支持的语言. ...
- emacs之配置7,tabbar插件
emacsConfig/tabbar-setting.el (require 'tabbar) (tabbar-mode ) (global-set-key (kbd "<M-up&g ...
- C#自带缓存方案
/// <summary> /// 获取数据缓存 /// </summary> /// <param name="CacheKey">键< ...
- C++ 构造函数_内存分区_对象初始化
内存分区 栈区:int x = 0:int *p = NULL; 定义一个变量,定义一个指针时,会在栈区进行分配内存.分配的内存系统分配收回的,我们不用管. 堆区:int *p = new i ...
- 学习笔记之Python 3 教程
Python 3 教程 http://www.runoob.com/python3/python3-tutorial.html Python的3.0版本,常被称为Python 3000,或简称Py3k ...
- servlet中获取配置文件中的参数.
web.xml (添加init-param) <?xml version="1.0" encoding="UTF-8"?> <web-app ...
- JMS消息模型
消息机制: 系统之间通信的中介,作为一台单独的服务器部署,大多数使用多个系统之间协作,是系统解耦的常见解决方案. 基于CS架构 作用:多个系统之间解耦,项目可以分开开发,满足显示的高可用(也可以说是异 ...