Unable to connect to oracle database from groovy

gshashank picture gshashank · Sep 27, 2013 · Viewed 14k times · Source

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.

Answer

topr picture topr · Sep 27, 2013

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.