Connecting to Oracle DB using Ruby - 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

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

how to load remote file into db2 database using jdbc

i'm trying to load from a local file into a db2 database using jdbc. the code looks like below:
stmt = jdbc.connection.prepareCall("call sysproc.db2load(....)")
stmt.regParameter(3, "load client from D:\\a.del of del insert into a")
but it doesn't work. The "SYSPROC.DB2LOAD" option is incompatible with the "CLIENT" option
so is there any way i can use jdbc to load local file into db2??

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 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 )

cx_Oracle connection by python3.5

I've tried several attempt to connect Oracle DB but still unable to connect. Following is my code to connect. However, I could connect Oracle DB through the terminal like this:
$ sqlplus64 uid/passwd#192.168.0.5:1521/WSVC
My evironment: Ubuntu 16.04 / 64bit / Python3.5
I wish your knowledge and experience associated with this issue to be shared. Thank you.
import os
os.chdir("/usr/lib/oracle/12.2/client64/lib")
import cx_Oracle
# 1st attempt
ip = '192.168.0.5'
port = 1521
SID = 'WSVC'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
# dsn_tns = cx_Oracle.makedsn(ip, port, service_name=SID)
db = cx_Oracle.connect('uid', 'passwd', dsn_tns)
cursor = db.cursor()
-------------------------------------------------
# 2nd attempt
conn = "uid/passwd#(DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.0.5)(PORT=1521)))(CONNECT_DATA=(SID=WSVC)(SRVR=DEDICATED)))"
db = cx_Oracle.connect(conn)
cursor = db.cursor()
------------------------------------------------------
# ERROR Description
cx_Oracle.InterfaceError: Unable to acquire Oracle environment handle
The error "unable to acquire Oracle environment handle" is due to your Oracle configuration being incorrect. A few things that should help you uncover the source of the problem:
when using Instant Client, do NOT set the environment variable ORACLE_HOME; that should only be set when using a full Oracle Client or Oracle Database installation
the value of LD_LIBRARY_PATH should contain the path which contains libclntsh.so; the value you selected looks like it is incorrect and should be /usr/lib/oracle/12.2/client64/lib instead
you can verify which Oracle Client libraries are being loaded by using the ldd command as in ldd cx_Oracle.cpython-35m-x86_64-linux-gnu.so

Resources