I am running the following code but I am getting the error about the name of the Oracle class. I have set the classpath environment variable with the oracle jar file but it doesn't work. Somebody could help me? I have no idea what else to do. I really appreciate any help
This is the error : Caused by: java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.io.jdbc import ReadFromJdbc
import os;
os.environ["JAVA_HOME"] = "/home/jupyter/env/java/jre1.8.0_291"
os.environ["PATH"] = "/home/jupyter/env/java/jre1.8.0_291/bin"
os.environ["ORACLE_HOME"] = "/home/jupyter/env/instantclient_21_1/"
os.environ["CLASSPATH"] = "/home/jupyter/env/ojdbc6-11.2.0.4.jar"
with beam.Pipeline(options=PipelineOptions()) as p:
result = (p
| 'Read from jdbc' >> ReadFromJdbc(
fetch_size=None,
table_name='log_table',
driver_class_name='oracle.jdbc.driver.OracleDriver',
jdbc_url='jdbc:oracle:thin://xx.x.xxx.xxx:xxxx/xxxxx',
username='xxxxxxxx',
password='xxxxxxxx',
))
could you please try the following suggestions?
Double-check the driver path location.
Validate the file permission, maybe it is necessary to execute as administrator.
Check that the database is up and running.
Validate port number and credentials.
Check this post, it contains great insights.
Related
I have this test code to connect to a SQL Server:
#GrabConfig(systemClassLoader=true)
#Grab(group='com.microsoft.sqlserver', module='mssql-jdbc', version='9.2.1.jre8')
import groovy.sql.Sql
def server = '10.6.6.1'
def port = '1433'
def user = 'sa'
def password = 'somepassword'
def url = "jdbc:sqlserver://${server}:${port};databaseName=master;"
Sql.withInstance(url, user, password) { sql ->
def serverName = sql.firstRow('SELECT ##SERVERNAME')
assert serverName[0]
}
if I run it I get:
Caught: java.sql.SQLException: No suitable driver found for jdbc:sqlserver://10.6.6.1:1433;databaseName=master;
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://10.6.6.1:1433;databaseName=master;
at test.run(test.groovy:12)
the jar for the driver is downloaded by Grape for sure because inside subdirectories in .groovy/ directory in my home dir I can find it.
But no way I am not able to connect to the server.
I am using groovy 3.0.9 but I tried with older versions and it's the same.
Edit:
If I add to the code before connecting:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
It works, but it's very strange, I was sure that this is not necessary anymore.
If someone can explain.
it still required to register sql driver in java.sql.DriverManager
every jdbc driver contains approximately the following code usually in XyzDriver class:
static {
try {
java.sql.DriverManager.registerDriver( new XyzDriver() )
} catch (SQLException e) {
...
}
}
the same for microsoft sql driver: https://github.com/microsoft/mssql-jdbc/blob/09d35bfc2338f1fc7c41a958d1e627fa0d6a2b65/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerDriver.java#L732
that's why you have to call a code like Class.forName("XyzDriver") to make driver self-register in DriverManager
UPD: JDBC 4.0 / java8+
from javadoc: https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html
JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver ...
When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.
mssql-jdbc-9.2.1.jre8.jar is 4.0 compatible. it contains com.microsoft.sqlserver.jdbc.SQLServerDriver in META-INF/services/java.sql.Driver file
however let's check DriverManager code and how it looks up for drivers:
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/sql/DriverManager.java#l100
static {
loadInitialDrivers();
println("JDBC DriverManager initialized");
}
DriverManager tries to find drivers at the moment it's loaded. so, jdbc driver must be present in classpath at app startup to be auto-registered.
and it's not the case with #Grab in code.
as workaround after grab you could do this to call self-register for all drivers:
ServiceLoader.load(java.sql.Driver.class).iterator().findAll()
I am trying to get data from SQL Server database table and show it as part of choice parameter as part of a Jenkins Job Build Parameters that I am trying to setup.
I am trying to figure out how to use Extensible Choice for this.
The Choice provider I used is "System Groovy Choice Parameter"
import groovy.sql.Sql
import com.microsoft.sqlserver.jdbc.SQLServerDriver
def output = []
def configuration = [
'dbInstance' : 'servername',
'dbPort' : 0000,
'dbName' : 'dbName',
'dbUser' : 'dbUser',
'dbPass' : 'dbPass'
]
def sql = Sql.newInstance("jdbc:sqlserver://${configuration.dbInstance}:${configuration.dbPort};"
+ "databaseName=" + configuration.dbName,
configuration.dbUser, configuration.dbPass,
'com.microsoft.sqlserver.jdbc.SQLServerDriver')
String sqlString = "SELECT * FROM dbTable"
sql.eachRow(sqlString){ row -> output.push(row[0])
}
return output.sort()
Below is the error I see. Which I understand I see because the jdbc driver is not present. I downloaded the driver from the link below:
https://www.microsoft.com/en-us/download/details.aspx?id=11774
I followed the instructions as to where it should be unzipped to as mentioned in the instructions.
I saw that the CLASSPATH variable is missing, so i went ahead and created the Environment variable with path: "C:\Program Files\sqljdbc_6.0\enu\sqljdbc.jar"
Error: unable to resolve class com.microsoft.sqlserver.jdbc.SQLServerDriver
How do i make sure that the script runs successfully and returns all the data to Extensible Choice. If there is anyother way to do this, I am open to suggestions.
Thank you very much
To resolve the issue I had to copy the "sqljdbc4.jar" file to the following location "C:\Java\jdk1.8.0_92\jre\lib\ext" since that is the path where the JAVA searches for the external jars. Use 4th version for the file which will have 4 in the file name as above as that is version Jenkins supports.
Environment:
Java API google-api-services-datastore-protobuf v1beta2-rev1-3.0.0.
OS: Windows 7.
Goal:
Start Local Datastore Server using the method:
public void start(String sdkPath, String dataset, String cmdLineOptions)
from com.google.api.services.datastore.client.LocalDevelopmentDatastore.java in order to use it in unit tests.
Steps:
I downloaded gcd tool gcd-v1beta2-rev1-3.0.2.zip and put it to C:\gcd folder
(paths to gcd.cmd and gcd.sh are `C:\gcd).
Also, I set environment variables:
"DATASTORE_HOST"="http://localhost:8080" and
"DATASTORE_DATASET"="myapp".
Problem:
LocalDevelopmentDatastoreException occurs.
Caused by: java.io.IOException: Cannot run program "./gcd.sh" (in directory "C:\gcd"): CreateProcess error=2, The system cannot find the file specified.
Note that it tries to find ./gcd.sh but not gcd.cmd.
Java code:
String datasetName = "myapp";
String hostName = "http://localhost:8080";
DatastoreOptions options = new DatastoreOptions.Builder()
.host(hostName)
.dataset(datasetName).build();
LocalDevelopmentDatastoreOptions localOptions = new LocalDevelopmentDatastoreOptions.Builder()
.addEnvVar("DATASTORE_HOST", hostName)
.addEnvVar("DATASTORE_DATASET", datasetName).build();
LocalDevelopmentDatastore datastore = LocalDevelopmentDatastoreFactory.get().create(options, localOptions);
datastore.start("C:\\gcd", datasetName);
This code is based on the example from LocalDevelopmentDatastore.java documentation.
Please help.
It seems as though the method is only programmed to look for gcd.sh, as it doesn't appear there's anything in your config which could have helped this to not fail. I suggest you open a defect report in the Cloud Platform Public Issue Tracker.
Did you consider gcloud-java for using the Datastore?
It also has an option for programmatically starting the local datastore using LocalGcdHelper which should work on Windows.
I am having errors with connecting to a MS Access Database (CDCollection.accdb) from Java. The IDE we are using is Netbeans. I have done research on possible solutions as to the error, but due to being inexperienced in the topic, I am not too sure how to solve it. The rest of my class are having the same problem and the teacher does no know how to solve the problem either.
My class is now in the final chapter, Learning Unit 10: Accessing a Database from a Java program, of Funworks's exploring IT: Java Programming. Grade 11.
I would post links to the three sources I have found most useful, but Stack Overflow says I need to have 10 reputation points to post more than 2 links. I have searched both Stack Overflow and the web to find the solution to this error, but those were more advanced than where I currently am.
Edit: Here are the links, thanks to those who upvoted me. The solution they suggest makes sense, but does not seem to work...
http://community.microfocus.com/borland/managetrack/starteam/w/knowledge_base/16933.error-java-sql-sqlexception-microsoft-odbc-driver-manager-data-source-name-not-found-and-no-default-driver-specified.aspx
http://www.coderanch.com/t/440574/JDBC/databases/java-sql-SQLException-ODBC-Driver
http://www.codeproject.com/Articles/35018/Access-MS-Access-Databases-from-Java
From what I understand, it is due to the school computers and my laptop all being 64-bit and ODBC (Open DataBase Connectivity) being 32-bit. My teacher does not know how to solve this problem, as it is her first time encountering it. When the grade 11s of last year reached this section of the book, the school computers were still 32-bit.
According to what I've read, I need to go to the OBDC Data Source Administrator and, under the System DSN tab, add "Microsoft Access Driver (*.mdb, *.accdb)". I noted that it was already there under the User DSN tab.
Find the OBDC Data Source Administrator at C:\Windows\SysWOW64\odbcad32.exe or Control Panel\System and Security\Administrative Tools\Data Sources (ODBC). The first is preferable, as it seems to have more Drivers available even though the Control Panel shortcut targets the same executable file.
However, I still get the same error once I have added the driver.
Here is my code:
The DB class
package cd;
import java.sql.*;
public class DB
{
private static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ={CDCollection.accdb}";
private Connection connection;
private PreparedStatement statement;
private ResultSet resultSet;
public DB()
{
try
{
Class.forName(driver);
System.out.println("Driver successfully loaded");
}
catch (ClassNotFoundException c)
{
System.out.println("Unable to load driver");
System.out.println(c);
}
try
{
connection = DriverManager.getConnection(url);
System.out.println("Connection successful");
}
catch (SQLException e)
{
System.out.println("Unable to connect");
System.out.println(e);
}
}
}
And the UseDB class:
package cd;
import java.sql.*;
public class UseDB
{
static DB db = new DB();
public static void main(String[] args)
{
}
}
For the
DBQ={CDCollection.accdb}
I have tried the full path to the file (C:\Users\\Documents\IT\Java\Databases\CD\CDCollection.accdb), using the double backslash due to the first marking an escape character. I have also tried with forward slashes (/) and I have tried without a path, as it is in the code, and placing the database in the \CD (the project folder), \CD\src (the source folder), \CD\src\cd (the package folder).
Yet, despite this, I and my classmates still get the same output every single we run the program:
run:
Driver successfully loaded
Unable to connect
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
BUILD SUCCESSFUL (total time: 0 seconds)
Could I please have help? Keep in mind that my class has only just started this module today; we have only been doing Access and SQL for 1 month and have been doing Java for a year and a half.
Thank you!
I would suggest you adding a SYSTEM DATASOURCE at ODBC configuration panel. Let's suppose you name it cdcollection. Then your url variable just needs to be:
private static final String url = "jdbc:odbc:cdcollection";
Here's a a nice STEP BY STEP article on how to do just that.
Welcome to StackOverflow.
I have the following Java code. Purpose of this code is to establish a connection
to a remote MySQL database ProductionDb ( a data source defined in my /etc/odbc.ini file ).
import java.sql.*;
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
Connection conn = null;
PreparedStatement s = null;
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver).newInstance();
conn = DriverManager.getConnection("jdbc:odbc:ProductionDb");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
The /etc/odbc.ini file is:
$ cat /etc/odbc.ini
[ProductionDb]
Driver = /usr/lib/odbc/libmyodbc.so
Description = Production Database
Server = [ hidden ]
Port = 3306
User = [ hidden ]
Password = [ hidden ]
Database = ProductionDb
By the way - I am using Java 7 and Ubuntu :
$java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
$lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 11.04
Release: 11.04
Codename: natty
When I try to run my program I get the following error:
$java Test
java: symbol lookup error: /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libJdbcOdbc.so: undefined symbol: SQLAllocEnv
Does anyone know why I get this error ?
What is wrong here ?
P.S By the way I did run sudo apt-get install unixodbc-dev , sudo apt-get install libmyodbc and sudo apt-get install libmysql-java :-)
UPDATE:
I have also tried the idea suggested in one of the replies below ( by Benny Hill ) : to use the /etc/odbcinst.ini as well as /etc/odbc.ini. Still doesn't work and I get the same error message.
$ cat /etc/odbc.ini
[ProductionDb]
Driver = MySQL Driver
Description = Production Database
Server = [ hidden ]
Port = 3306
User = [ hidden ]
Password = [ hidden ]
Database = ProductionDb
$ cat /etc/odbcinst.ini
[MySQL Driver]
Driver = /usr/lib/odbc/libmyodbc.so
ADDITIONAL NOTE:
I can use this ODBC data source successfully from the R
programming language.
> library(odbc)
> con = odbcConnect("ProductionDb")
> con
RODBC Connection 1
Details:
case=nochange
DSN=ProductionDb
The error is the result of libJdbcOdbc.so looking for the function "SQLAllocEnv" in some other .so and not finding it. The way to debug this is to run the command ldd /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libJdbcOdbc.so. That will show you a list of linked .so objects and where they are located.
Generally speaking they should be in /usr/lib however if you have compiled any software yourself you may find that some of these libs are in /usr/local/lib or some other location. If you have anything that shows up in /usr/local/lib it's possible this is what's causing your problem. To test, rename the library in /usr/local/lib to something else (sudo mv /usr/local/lib/mylib.so /usr/local/lib/mylib.so.SAVE).
Now run your program and see if you still get the same error. If that fixes your problem then great! If not, let us know if you get the same error message or if you get a new one.
I would expect your odbc.ini file to look like this:
[primary]
Description = primary
Driver = iSeries Access ODBC Driver
System = XXX.XXX.XXX.XXX
UserID = XXXXXXXXXX
Password = XXXXXXXXXX
Naming = 0
DefaultLibraries = QGPL
Database = XXXXXXXXXX
ConnectionType = 0
CommitMode = 2
ExtendedDynamic = 0
DefaultPkgLibrary = QGPL
DefaultPackage = A/DEFAULT(IBM),2,0,1,0,512
AllowDataCompression = 1
LibraryView = 0
AllowUnsupportedChar = 0
ForceTranslation = 0
Trace = 0
And your odbcinst.ini file to look like this:
[iSeries Access ODBC Driver]
Description = iSeries Access for Linux ODBC Driver
Driver = /usr/lib/libcwbodbc.so
Setup = /usr/lib/libcwbodbcs.so
NOTE1 = If using unixODBC 2.2.11 or later and you want the 32 and 64-bit ODBC drivers to share DSN's,
NOTE2 = the following Driver64/Setup64 keywords will provide that support.
Driver64 = /usr/lib/lib64/libcwbodbc.so
Setup64 = /usr/lib/lib64/libcwbodbcs.so
Threading = 2
DontDLClose = 1
UsageCount = 1
My example shows my setup for a remote iSeries but I'm sure you can see what you would need to change for MySQL.
Namely your odbc.ini "Driver = ..." line is wrong. It should be something like "Driver = mysql" and then you need to define [mysql] in your odbcinst.ini file.
Sounds like a missing or mismatched library. Try to debug the ldd processing.
First, check what
$ ldd /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libJdbcOdbc.so
says, do all listed dependencies exist?
Then, try setting LD_DEBUG and and start your Java program again, to see the loader debug.
$ export LD_DEBUG=all
$ java Test
To fix this add the following to your startup script or profile:
export LD_PRELOAD=$LD_PRELOAD:/usr/lib/libodbc.so:/usr/lib/libodbcinst.so
Your path may vary a bit, for me the .so files where in /usr/lib64.
"There is a bug due to the fact that the libraries "libodbc.so" and "libodbcinst.so" libJdbcOdbc.so are not loaded from the library that implements the JDBC-ODBC bridge."
According to https://code.google.com/p/queryconnector/source/browse/wiki/HowToInstall.wiki?spec=svn122&r=121
I've used MySQL with JDBC before, and the easiest way I know to connect is using Connector/J which is the official MySQL driver. It will do the same as your ODBC driver, and doesn't require native stuff which may be causing your problem.
Get the driver from here: http://dev.mysql.com/downloads/connector/j/
Docs on above page.
Docs how to connect: http://dev.mysql.com/doc/refman/5.1/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager
Go to the directory:
/usr/lib/jvm/java-7-oracle/jre/lib/amd64/
And rename the file libJdbcOdbc.so to libJdbcOdbcXXX.so
This worked for me.
As a workaround and if SQLAllocEnv is defined in /usr/lib/odbc/libmyodbc.so you could try to force loading it before using JDBC
try {
System.load("/usr/lib/odbc/libmyodbc.so");
} catch (UnsatisfiedLinkError e) {
e.printStackTrace();
}
export LD_PRELOAD=$LD_PRELOAD:/usr/lib/libodbc.so:/usr/lib/libodbcinst.so
once I define this in .bash_profile, source it and it works fine for me.