Unable to connect to oracle database from groovy

为君一笑 提交于 2021-02-10 09:30:07

问题


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.


回答1:


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.




回答2:


I had a few issues connecting Groovy to Oracle.

I tried grabbing ojdbc14.jar using

https://mvnrepository.com/artifact/com.oracle/ojdbc14/10.2.0.3.0

// https://mvnrepository.com/artifact/com.oracle/ojdbc14
@Grapes(
    @Grab(group='com.oracle', module='ojdbc14', version='10.2.0.3.0')
)

but it did not find the file.

I copied ojdbc14.jar from my Oracle Client Directory to %GROOVY_HOME%\lib and it worked like a charm. I am sure you can download the ojdbc14.jar from the internet if you don't have the Oracle SQL Client Installed.

copy C:\DevSuiteHome_1\jdbc\lib\ojdbc14.jar C:\groovy-2.4.11\lib

url= "jdbc:oracle:thin:@localhost:1521:SID"
username = "un"
password = "pw"
driver = "oracle.jdbc.driver.OracleDriver"

// Groovy Sql connection test
import groovy.sql.*
sql = Sql.newInstance(url, username, password, driver)
try {
    sql.eachRow('select sysdate from dual'){ row ->
    println row
 }
 } finally {
     sql.close()
 }

Hope that helps.



来源:https://stackoverflow.com/questions/19049194/unable-to-connect-to-oracle-database-from-groovy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!