groovy 无法连接mysql,无法从Groovy连接到Oracle数据库

Hi i am unable to connect to oracle database in groovy . I have used the following code in groovy console but getting the following compilation error

unable to resolve class oracle.jdbc.driver.OracleTypes

at line: 5, column: 1

I have used the following code

import java.sql.Connection

import java.sql.DriverManager

import javax.sql.DataSource

import groovy.sql.Sql

import oracle.jdbc.driver.OracleTypes

sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:databasename",

"username", "password", "oracle.jdbc.OracleDriver")

If i remove import oracle.jdbc.driver.OracleTypes statement i am getting the following WARNING: Sanitizing stacktrace:.Kindly help me how to resolve this i have place ojdbc14.jar in the lib folder.

解决方案

Remove all unnecessary imports and driver class from newInstance call as follows:

import groovy.sql.Sql

sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:databasename", "username", "password")

The above is enough and works just fine for me, however I'm using ojdbc6-11.2.jar

Beside depends on if you are using SID or service name the last semicolon at JDBC URL might have to be changed for slash.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Groovy连接多个数据库进行数据操作,可以使用不同的数据库驱动程序和连接池。以下是一个示例代码,演示如何连接两个数据库MySQLOracle)并执行数据操作: ```groovy @Grab(group='org.codehaus.groovy.modules', module='groovy-sql', version='3.0.0') import groovy.sql.Sql import groovy.sql.SqlDataSource // MySQL 数据库连接信息 def mysqlUrl = 'jdbc:mysql://localhost:3306/mydb' def mysqlUsername = 'root' def mysqlPassword = 'root' // Oracle 数据库连接信息 def oracleUrl = 'jdbc:oracle:thin:@localhost:1521:xe' def oracleUsername = 'system' def oraclePassword = 'oracle' // 创建 MySQL 数据库连接池和 Sql 对象 def mysqlDataSource = new SqlDataSource( url: mysqlUrl, username: mysqlUsername, password: mysqlPassword, driver: 'com.mysql.jdbc.Driver' ) def mysql = Sql.newInstance(mysqlDataSource) // 创建 Oracle 数据库连接池和 Sql 对象 def oracleDataSource = new SqlDataSource( url: oracleUrl, username: oracleUsername, password: oraclePassword, driver: 'oracle.jdbc.driver.OracleDriver' ) def oracle = Sql.newInstance(oracleDataSource) // 在 MySQL 中插入数据 mysql.execute("INSERT INTO users (username, password) VALUES ('john', 'doe')") // 在 Oracle 中查询数据 def result = oracle.rows("SELECT * FROM orders WHERE customer_id = 123") // 关闭连接 mysql.close() oracle.close() ``` 在上面的示例中,我们使用了 `groovy-sql` 模块来连接数据库和执行数据操作。我们首先定义了两个数据库连接信息(URL、用户名、密码和驱动程序),然后创建了两个 `SqlDataSource` 对象,分别用于连接 MySQLOracle 数据库。接着,我们使用 `Sql.newInstance` 方法来创建两个 `Sql` 对象,用于执行数据操作。最后,我们在 MySQL 数据库中插入了一条数据,然后在 Oracle 数据库中查询了一些数据。最后,我们使用 `close` 方法关闭了连接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值