Unable to connect to oracle database from groovy - jdbc

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.

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.

Related

How to install JDBC driver on Databricks Cluster?

I'm trying to get the data from my Oracle Database to a Databricks Cluster. But I think I'm doing it wrong:
On the cluster library I just installed the ojdbc8.jar and then after that I opened a notebook and did this to connect:
CREATE TABLE oracle_table
USING org.apache.spark.sql.jdbc
OPTIONS (
dbtable 'table_name',
driver 'oracle.jdbc.driver.OracleDriver',
user 'username',
password 'pasword',
url 'jdbc:oracle:thin://#<hostname>:1521/<db>')
And it says:
java.sql.SQLException: Invalid Oracle URL specified
Can someone help? I've been reading documentations but there's no clear instruction on how I should actually install this jar step by step. I might be using the wrong jar? Thanks!
I have managed to set this up in Python/PySpark as follows:
jdbcUrl = "jdbc:oracle:thin:#//hostName:port/databaseName"
connectionProperties = {
"user" : username,
"password" : password,
"driver" : "oracle.jdbc.driver.OracleDriver"
}
query = "(select * from mySchema.myTable )"
df = spark.read.jdbc(url=jdbcUrl, table=query1, properties=connectionProperties)
I am using the Oracle JDBC Thin Driver instantclient-basic-linux.x64-21.5.0.0.0, as available on the Oracle webpages. The current version is 21.7 I think, but it should work the same way.
Check this link to understand the two different notations for jdbc URLs

Groovy java.sql.SQLException: No suitable driver found for jdbc:sqlserver://

I am quite new to groovy and am quite confused about the Groovy java.sql.SQLException that I'm getting.
Here is my code
// https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc
#Grapes(
#Grab(group='com.microsoft.sqlserver', module='mssql-jdbc', version='7.2.2.jre8')
)
import groovy.sql.*
def username = xxx, password = yyy
// Create connection to MSSQL with classic JDBC DriverManager.
def db = Sql.newInstance("jdbc:sqlserver://localhost:1433;database=tempdb;", username, password, 'com.microsoft.sqlserver.jdbc.SQLServerDriver')
The same SQL JDBC Connection string ("jdbc:sqlserver://localhost:1433;database=tempdb;", 'com.microsoft.sqlserver.jdbc.SQLServerDriver') works just fine for my other cases, like Java or JMeter, but not works with groovy. This is what I'm getting:
> groovy groovy-sql-test.groovy
Picked up _JAVA_OPTIONS: -Xms512M -Xmx1g
Caught: java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;database=tempdb;
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:1433;database=tempdb;
at groovy-sql-0.run(groovy-sql-0.groovy:11)
This is running under Win10.
I've also tried
groovy -cp D:\path\to\my\jars groovy-sql-test.groovy
where within the D:\path\to\my\jars dir, I'm having both sqljdbc41.jar and mssql-jdbc-7.2.2.jre8.jar files.
You have to use #GrabConfig(systemClassLoader=true) to use the system
classloader and thus get the jdbc driver picked up.
From https://groovy-lang.org/databases.html#_connecting_using_grab
The #GrabConfig statement is necessary to make sure the system
classloader is used. This ensures that the driver classes and system
classes like java.sql.DriverManager are in the same classloader.

groovy oracle - No suitable driver found

Groovy 2.4.12
Oracle Express 11.2.0.2
ojdbc6.jar
I've just installed Oracle Express created a new user with all privileges. I can connect to my xe instance from SQL Developer, so I know its running.
Groovy console, I've added ojdbc6.jar to the classpath and now trying this...
import groovy.sql.Sql;
def cl = Class.forName('oracle.jdbc.OracleDriver')
println cl // outputs 'class oracle.jdbc.OracleDriver'
def db = [
url: 'jdbc:oracle:thin:#localhost:1521:xe',
user: 'me',
password: 'me',
driver: 'oracle.jdbc.OracleDriver']
def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
I get the SQLException "No suitable driver found for jdbc:oracle:thin:#//localhost:1521/xe".
For the connect string I've also tried jdbc:oracle:thin:#localhost:1521:xe and I've also tried oracle.jdbc.driver.OracleDriver for both loading and the driver parameter.
Is this combo not possible or have I missed something obvious?
EDIT:
Correct connect string is the no-slashes format.
Turns out #tim_yates was almost there.
Needed this...
def f = new File('c:\\oraclexe\\app\\oracle\\product\\11.2.0\\server\\jdbc\\lib\\ojdbc6.jar')
this.getClass().classLoader.rootLoader.addURL(f.toURL())
No idea why MySQL worked without this!

How do I connect to a Netcool / Omnibus “Object Server” using JayDeBeApi module along with SAP Sybase JDBC drivers (jconn4.jar) in Python3?

I am new to python programming. I'm trying to connect to a Netcool Object Server using Python3, I am using JayDeBeApi module along with SAP Sybase JDBC drivers (jconn4.jar)
following is the sample script:
import jaydebeapi
server="xxx"
database="xx"
user="xx"
password="xx"
jclassname='com.sybase.jdbc4.jdbc.SybDriver'
url='jdbc:sybase:Tds://'+server+'/'+database
driver_args=[url,user,password]
jars="path/jconn4.jar"
conn=jaydebeapi.connect(jclassname,driver_args,jars)
curs = conn.cursor()
curs.execute("select * from status")
curs.fetchall()`
when I am executing the script it showing an error as follows
File "sample.py", line 12, in <module>
conn=jaydebeapi.connect(jclassname,driver_args,jars)
File "/usr/local/lib/python3.5/site-packages/jaydebeapi/__init__.py", line 381, in connect
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
File "/usr/local/lib/python3.5/site-packages/jaydebeapi/__init__.py", line 199, in _jdbc_connect_jpype
return jpype.java.sql.DriverManager.getConnection(url, *dargs)
RuntimeError: No matching overloads found. at native/common/jp_method.cpp:117
if anyone successfully connected to a Netcool Object Server using JayDeBeApi module in Python3? please share the sample script
thanks
The url format you specified is not correct. The below works for me.
url = jdbc:sybase:Tds:++hostname:++dbport/++dbname
e.g
conn = jaydebeapi.connect('com.sybase.jdbc4.jdbc.SybDriver', ['jdbc:sybase:Tds:hostA:8888/db1','root',''],['path/jconn4.jar'])

Connecting to oracle database located remotely

I am trying to run following code which is connecting to remote database and retrieving records:
import java.sql.*;
class Employee
{
public static void main (String args [])
throws SQLException, ClassNotFoundException {
// Load the Oracle JDBC driver
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// Connect to the database
// You must put a database name after the # sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:#ourcompany.com:1521:course", "username", "password");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select * from test");
// Iterate through the result and print the employee names
/*
while (rset.next ())
System.out.println (rset.getString ("name"));
System.out.println (rset.getString ("id"));
*/
rset.next();
System.out.println(rset.getString("name"));
}
}
after running this code from Netbeans i am getting an error:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#ourcompany.com:1521:course
at java.sql.DriverManager.getConnection(DriverManager.java:604)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at Employee.main(Emplyoee.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
I have downloaded ojdbc14.jar and kept in C:\Program Files\Java\jdk1.7.0\jre\lib path.
I do not know where i am going wrong?...plz help me here.
You are using awfully old version of Oracle JDBC driver. You should be using ojdbc6.jar.
Try with this driver:
Class.forName ("oracle.jdbc.OracleDriver");
Check your classpath in Netbeans:
How to set classpath in NetBeans:
In NetBeans Project Properties Window, you click Libraries in the left panel, and in the right panel are 4 categories of classpath you can configure:
Compile: Empty by default. Compile-time libraries are automatically
propagated to other categories of classpath, so you don't need to
repeat the same set of jar files in all 4 categories.
Run: By default includes everything in compile-time classpath, and
compiled classes (e.g., build/classes).
Compile Tests: By default includes everything in compile-time
classpath, compiled classes (e.g., build/classes), and JUnit.
Run Tests: By default includes classpath for compiling tests, and
compiled tests.

Resources