[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()
}
时间: 2024-10-10 10:28:22

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

DATABASE CONNECTION ERROR (1): THE MYSQL ADAPTER &#39;MYSQLI&#39; IS NOT AVAILABLE.解决办法

网站迁移之后遇到:Database connection error (1): The MySQL adapter 'mysqli' is not available.这个问题,我一开始以为是我配置的参数错了呢!然后回去检查,检查好几遍我的数据库账号和密码都没错,但是为什么就是连接不上数据库呢?? 网上有人说是"可能有你安装wamp或者xamp之后,sqlserver的服务没有启动,你可以到系统服务菜单里看看这个是否自动启动."但是我问过空间商,他们说默认都是开启的,然后他们也检查了数

MySQL Strict Mode is not set for database connection &#39;default&#39;

SET GLOBAL sql_mode='strict_trans_tables'; SET sql_mode ='strict_trans_tables';   MySQL Strict Mode is not set for database connection 'default'

解决Kloxo出现Could not open database connection问题

当我们在使用或者运行kloxo面板的时候,可能会出现类似"Could not open database connection"错误提示,对于新手朋友来说肯定本身安装面板管理VPS就想让事情简单一点.如果再出现这样的问题,势必会使得我们更加不知道如何处理.如果是主机产品还可以找主机商或者主机面板,但是VPS是独立的产品,所有的问题都需要我们自己来解决. 解决方法如下: 用SSH登陆我们的VPS,然后输入下面两行命令 php /usr/local/lxlabs/kloxo/bin/com

解决:wordpress error establishing a database connection problem

我是个网站菜鸟,刚开始搭建LAMP环境的时候,就要了我半条老命. 没办法,懂的东西太少,LAMP是什么我都不懂,域名是什么,我也被不懂,为什么想要有个网站就要有服务器我还是不懂.一步步地自己去钻,去看digitalocean给的说明教程.慢慢就懂了. 我的VPS(Virtual Private Server虚拟专用服务器)的配置很low,CPU只有512M- - 最开始解决这个问题,就是重启我的服务器 reboot httpd -k restart 以上两个代码用的飞起,实际上如果是databa

django:ySQL Strict Mode is not set for database connection &#39;default&#39;

?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'        HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly re

Explain tables does not exist. Check the explain plan properties for this database connection.

使用DbVisulalizer链接db2,查看执行计划是报错误 就是执行计划的表不村子,检查执行计划属性. 查看连接属性如下 只有DB2LUW里面才有Explain Plan属性,所以先调整Connection属性中的datatype,为DB2 LUW: 然后再用,发现还是一样的错误,最后仔细看,才发现有个勾没有勾上 只有勾上了create plan table if not exists,就是如果计划表不存在就创建.然后解决问题. 当然我们也可以把下面的Drop Explain Plan Ta

[Training Video - 5] [Groovy Script Test Step - Collections, Exceptions] Exception Handling in groovy

def x = new String[3] x[0] = "A" x[1] = "B" x[2] = "C" log.info"XXXXXX 1" try{ x[3] = "D" // def z=9/0 }catch(Exception e){ log.info "Some error "+e.getMessage() // Use e.getMessage() to print ex

[Training Video - 4] [Groovy] Optional parameter in groovy

Employee.log=log Employee e1 = new Employee() log.info e1.add(1,2,3,4) // optional parameters in groovy log.info e1.add(2,3) log.info e1.add(2,3,10) class Employee { def static log public def add(a,b,c=3,d=10){ return a+b+c+d } } Run result: Tue Oct

[Training Video - 4] [Groovy] Constructors in groovy, this keyword

Bank.log = log Bank b1 = new Bank() b1.name = "BOA" b1.minbalance = 100 b1.city="London" Bank b2 = new Bank() b2.name = "HSBC" b2.minbalance = 100 b2.city="LA" Bank b3 = new Bank("A",100,"X") log