博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Training Video - 7] [Database connection] Part 1
阅读量:4682 次
发布时间:2019-06-09

本文共 2088 字,大约阅读时间需要 6 分钟。

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

 

转载于:https://www.cnblogs.com/MasterMonkInTemple/p/4860950.html

你可能感兴趣的文章
cache—主存—辅存三级调度模拟
查看>>
Java线程的定义
查看>>
Python-面向对象(组合、封装与多态)
查看>>
Mininet
查看>>
COSC2531 Programming Fundamentals
查看>>
设计模式系列 - 访问者模式
查看>>
20180507小测
查看>>
eclipse左侧不见
查看>>
python会缓存小的整数和短小的字符
查看>>
格网与四叉树索引
查看>>
多张照片拍摄、图片浏览
查看>>
html(5) css
查看>>
Azure Web连接到Azure MySql Db
查看>>
Linux shell 命令判断执行语法 ; , && , ||
查看>>
vim代码格式化插件clang-format
查看>>
windows registry
查看>>
jquery 动画总结(主要指效果函数)
查看>>
leetcode-17-电话号码的字母组合’
查看>>
Flume 示例
查看>>
Designing for Performance
查看>>