使用Groovy连接到MySQL [英] Connecting to MySQL using Groovy

查看:923
本文介绍了使用Groovy连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Mac OS 10.10.5 Yosemite上使用下面的Groovy代码连接到MySQL数据库

  import groovy.sql.Sql 

尝试{
def dbURL ='jdbc:mysql:// localhost:3306 / sakila'
def dbUserName ='root'
def dbPassword ='Orange @ 27'
def dbDriver ='com.mysql.jdbc.Driver'
log.info('Good')
def db = Sql.newInstance(dbURL,dbUserName, db $ db $ b $ catch $ {
log.info('DB Error')
log.info(e.getMessage())
} finally {

}

但是当我执行这段代码时,我看到下面的消息

  8月13日星期六15:09:14 EDT 2016:信息:好
8月13日星期六15:09:14 EDT 2016:信息:数据库错误
Sat Aug 13 15:09:14 EDT 2016:INFO:找不到适合jdbc的驱动程序:mysql:// localhost:3306 / sakila

我保留了 groovy-sql-2.1.1.jar MySQL的连接器的Java -5.1.39-bin.jar 位于文件夹位置 /Applications/SoapUI-5.2.1/bin/ext /



您可以帮我解决这个问题吗?

解决方案

JDBC驱动程序需要注册它。有许多方法可以做到这一点:

使用DriverManager



您可以注册一个JDBD驱动程序 DriverManager

  import java.sql.DriverManager 

DriverManager.registerDriver(new com.mysql.jdbc.Driver())

//剩余的Groovy代码



Class.forName



一种黑客,但您也可以动态加载驱动程序的类:

  Class.forName('com.mysql.jdbc.Driver')

//剩余的Groovy代码



Groovy Grape



因为您使用Soap UI运行Groovy代码,这个选项可能不适合你,但在这里是为了完整性:

  @Grab('mysql: mysql-connector-java:5.1.39')
@GrabConfig(systemClassLoader = true)

//剩余的Groovy代码


I'm trying to connect to a MySQL database using the below Groovy code on MAC OS 10.10.5 Yosemite

import groovy.sql.Sql

try{
    def dbURL = 'jdbc:mysql://localhost:3306/sakila'
    def dbUserName = 'root'
    def dbPassword = 'Orange@27'
    def dbDriver = 'com.mysql.jdbc.Driver'
    log.info('Good')
    def db = Sql.newInstance(dbURL,dbUserName,dbPassword,dbDriver)
}catch(Exception e){
    log.info('DB Error')
    log.info(e.getMessage())
}finally{

}

But when I execute this code, I see the below message

Sat Aug 13 15:09:14 EDT 2016:INFO:Good
Sat Aug 13 15:09:14 EDT 2016:INFO:DB Error
Sat Aug 13 15:09:14 EDT 2016:INFO:No suitable driver found for jdbc:mysql://localhost:3306/sakila

I have kept the groovy-sql-2.1.1.jar, mysql-connector-java-5.1.39-bin.jar inside the folder location /Applications/SoapUI-5.2.1/bin/ext/

Could you please help me in resolving this issue?

解决方案

In order to use a JDBC driver you need to register it. There are a number of ways to do this:

Using DriverManager

You can register a JDBD driver with a DriverManager:

import java.sql.DriverManager

DriverManager.registerDriver(new com.mysql.jdbc.Driver())

// Remaining Groovy code here

Class.forName

Kind of a hack, but you can also dynamically load the driver's class:

Class.forName('com.mysql.jdbc.Driver')

// Remaining Groovy code here

Groovy Grape

Because you're using Soap UI to run the Groovy code, this option may not work for you, but here it is for completeness:

@Grab('mysql:mysql-connector-java:5.1.39')
@GrabConfig(systemClassLoader=true)

// Remaining Groovy code here

这篇关于使用Groovy连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆