当前位置:K88软件开发文章中心编程语言JavaGroovy → 文章内容

Groovy 数据库

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:50:01

由 lichiabor 创建,小路依依 最后一次修改 2016-12-12 Groovy的groovy-sql模块提供了比当前Java的JDBC技术更高级的抽象。Groovy sql API支持各种各样的数据库,其中一些如下所示。 HSQLDB OracleSQL Server MySQL MongoDB在我们的示例中,我们将使用MySQL DB作为示例。为了使用MySQL与Groovy,首先要做的是从mysql站点下载MySQL jdbc jar文件。 MySQL的格式如下所示。 mysql-connector-java-5.1.38-bin然后,确保将上述jar文件添加到工作站中的类路径。数据库连接在连接到MySQL数据库之前,请确保以下内容 - 你已经创建了一个数据库TESTDB。 您在TESTDB中创建了一个表EMPLOYEE。此表格包含FIRST_NAME,LAST_NAME,AGE,SEX和INCOME。用户ID“testuser”和密码“test123”设置为访问TESTDB。确保已下载mysql jar文件并将该文件添加到类路径。你已经通过MySQL教程来了解MySQL基础 下面的示例显示如何连接MySQL数据库“TESTDB”。import java.sql.*; import groovy.sql.Sql class Example { static void main(String[] args) { // Creating a connection to the database def sql = Sql.newInstance('jdbc:mysql://localhost:3306/TESTDB', 'testuser', 'test123', 'com.mysql.jdbc.Driver') // Executing the query SELECT VERSION which gets the version of the database // Also using the eachROW method to fetch the result from the database sql.eachRow('SELECT VERSION()'){ row -> println row[0] } sql.close() } } 运行此脚本时,会产生以下结果 - 5.7.10-log The Sql.newInstance method is used to establish a connection to the database.创建数据库表连接到数据库后的下一步是在数据库中创建表。以下示例显示如何使用Groovy在数据库中创建表。Sql类的execute方法用于对数据库执行语句。import java.sql.*; import groovy.sql.Sql class Example { static void main(String[] args) { // Creating a connection to the database def sql = Sql.newInstance('jdbc:mysql://localhost:3306/TESTDB', 'testuser', 'test123', 'com.mysql.jdbc.Driver') def sqlstr = """CREATE TABLE EMPLOYEE ( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )""" sql.execute(sqlstr); sql.close() } } 插入操作当您要将记录创建到数据库表中时需要。例子以下示例将在employee表中插入一条记录。代码放置在try catch块中,因此如果记录成功执行,事务将提交到数据库。如果事务失败,则执行回滚。import java.sql.*; import groovy.sql.Sql class Example { static void main(String[] args) { // Creating a connection to the database def sql = Sql.newInstance('jdbc:mysql://localhost:3306/TESTDB', 'testuser', 'test123', 'com.mysql.jdbc.Driver') sql.connection.autoCommit = false def sqlstr = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" try { sql.execute(sqlstr); sql.commit() println("Successfully committed") }catch(Exception ex) { sql.rollback() println("Transaction rollback") } sql.close() } }假设您只想根据条件选择某些行。以下代码显示如何添加参数占位符以搜索值。上面的例子也可以写成参数,如下面的代码所示。 $符号用于定义一个参数,然后可以在执行sql语句时将其替换为值。import java.sql.*; import groovy.sql.Sql class Example { static void main(String[] args) { // Creating a connection to the database def sql = Sql.newInstance('jdbc:mysql://localhost:3306/TESTDB', 'testuser', 'test123', 'com.mysql.jdbc.Driver') sql.connection.autoCommit = false def firstname = "Mac" def lastname ="Mohan" def age = 20 def sex = "M" def income = 2000 def sqlstr = "INSERT INTO EMPLOYEE(FIRST_NAME,LAST_NAME, AGE, SEX, INCOME) VALUES " + "(${firstname}, ${lastname}, ${age}, ${sex}, ${income} )" try { sql.execute(sqlstr); sql.commit() println("Successfully committed") } catch(Exception ex) { sql.rollback() println("Transaction rollback") } sql.close() }}读操作读操作上的任何数据库是指从数据库中获取一些有用的信息。一旦我们的数据库建立连接,您就可以进行查询到这个数据库中。 读出操作是通过使用SQL类的eachRow方法进行。 句法eachRow(GString gstring, Closure closure) 执行给定的SQL查询调用,结果集的每一行给出闭幕。 参数 GString的 -这需要执行的SQL语句。 封闭 -封闭语句来处理来自读操作retrived行。执行给定的SQL查询调用,结果集的每一行给出闭幕。 下面的代码示例演示了如何来从雇员表中的所有记录。 import java.sql.*; import groovy.sql.Sql class Example { static void main(String[] args) { // Creating a connection to the database def sql = Sql.newInstance('jdbc:mysql://localhost:3306/TESTDB', 'testuser', 'test123', 'com.mysql.jdbc.Driver') sql.eachRow('select * from employee') { tp -> println([tp.FIRST_NAME,tp.LAST_NAME,tp.age,tp.sex,tp.INCOME]) } sql.close() } }从上面的程序的输出将是 - [Mac, Mohan, 20, M, 2000.0] 更新操作任何数据库上进行更新操作手段来更新一个或多个记录,这已经在数据库中。下面的过程更新过性生活为“M”的所有记录。在这里,我们一岁增加所有男子的年龄。 import java.sql.*; import groovy.sql.Sql class Example { static void main(String[] args){ // Creating a conne

[1] [2]  下一页


Groovy 数据库