groovy oracle - No suitable driver found - oracle

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!

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.

How to load table from SQL server using H2o in R?

I try to load table into R using h2o but had the following error
my_data <- h2o.import_sql_table(my_sql_conn, table, username, password)
ERROR: Unexpected HTTP Status code: 500 Server Error (url = http://localhost:54321/99/ImportSQLTable)
java.lang.RuntimeException [1] "java.lang.RuntimeException: SQLException: No suitable driver found for jdbc:mysql://10.140.20.29/MySQL?&useSSL=false\nFailed to connect and read from SQL database with connection_url: jdbc:mysql://10.140.20.29/MySQL?&useSSL=false"
Can someone help me with this? Thank you so much!
You need a supported JDBC (Build on JDBC 42 Core) driver to connect from H2O to SQL Server. You can download Microsoft JDBC Driver 4.2 for SQL Server from the link below first:
https://www.microsoft.com/en-us/download/details.aspx?id=54671
After that please follow the article below to first test JDBC driver from R/Python H2O client and then connect to your database:
https://aichamp.wordpress.com/2017/03/20/building-h2o-glm-model-using-postgresql-database-and-jdbc-driver/
Above article is for postgres however you can use it with SQL server using an appropriate driver.
For Windows, remember to use ; instead : for the -cp argument.
java -Xmx4g -cp sqljdbc42.jar;h2o.jar water.H2OApp -port 3333
water.H2OApp is the main class in h2o.jar.
Important Note: SQL Server is not supported so far( August/2017).
You may use MariaDB to load datasets:
From Windows console:
java -Xmx4G -cp mariadb-java-client-2.1.0.jar;h2o.jar water.H2OApp -port 3333
Note. For Linux, replace ";" with ":"
From R:
sqlConn <- "jdbc:mariadb://10.106.7.46:3306/DBName"
userName <- "dbuser"
userPass <- "dbpass."
sql_Query <- "SELECT * FROM dbname.tablename;"
mydata <- h2o.import_sql_select( sqlConn, sql_Query, userName, userPass )

Unable to connect to oracle database from groovy

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.

Connecting to Oracle DB using Ruby

I am stuck with connecting to Oracle DB, have read lots of stuff but no help on result.
I have remote Oracle DB, I am connecting to it using DBVisualizer setting connection like this:
DB Type : Oracle
Driver (jdbc) : Oracle thin
Database URL: jdbc:oracle:thin:#10.10.100.10:1521/VVV.LOCALDOMAIN
UserIdf: SomeUser
Pass: SomePass
Connection works ok.
What I do in Ruby is :
require 'oci8'
require 'dbi'
...
conn = OCI8.new('SomeUser','SomePass','//10.10.100.10:1521/VVV.LOCALDOMAIN')
...
What I get is:
ORA-12545: Connect failed because target host or object does not exist
oci8.c:360:in oci8lib.so
the third parameter needs to be the TNS hostname, if you use SQL plus it is also the third parameter in the connectstring, you can find it also in the tnsnames.ora file in the oracle maps
in SQLPlus : connect user/password#hostname;
in oci8 : conn = OCI8.new('SomeUser','SomePass',hostname)
Here a working sample, obfuscated the parameters of course
require 'oci8'
oci = OCI8.new('****','***','****.***')
oci.exec('select * from table') do |record|
puts record.join(',')
end

Resources