Getting oracle.jdbc.driver.LogicalConnection, need oracle.jdbc.OracleConnection - java

I am trying to connect to an Oracle database inside a Java application running on WebSphere. I need to be able to create an array descriptor to use in a call to a procedure.
The code looks like this:
Connection conn=null;
ArrayDescriptor arrayDescriptor;
Connection tmpCon = jdbcTemplate.getDataSource().getConnection();
conn = WSCallHelper.getNativeConnection(tmpCon);
arrayDescriptor = ArrayDescriptor.createDescriptor("t_my_array",conn);
IDs = new oracle.sql.ARRAY(arrayDescriptor, conn, list.toArray());
The line that calls ArrayDescriptor.createDescriptor throws a class cast exception
java.lang.ClassCastException: oracle.jdbc.driver.LogicalConnection incompatible with oracle.jdbc.OracleConnection
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:149)
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:115)
Walking through this with the debugger, I can see that conn is definitely a oracle.jdbc.driver.LogicalConnection. The problem is I can't actually reference LogicalConnection in my code because that class is not public, so I can't just do something like this:
arrayDescriptor = ArrayDescriptor.createDescriptor("t_my_array",((LogicalConnection)conn).getWrapper());
.
And this:
arrayDescriptor = ArrayDescriptor.createDescriptor("t_my_array",((oracle.jdbc.driver.OracleConnection)conn).getWrapper());
also returns a class cast exception:
java.lang.ClassCastException: oracle.jdbc.driver.LogicalConnection incompatible with oracle.jdbc.driver.OracleConnection
I need to have an OracleConnection object, but I can't seem to get one from the LogicalConnection that gets returned to me. Has anyone ever seen this before? I feel I'm missing something really obvious here, but maybe I just need another cup of coffee...

#Alex Poole pointed me in the right direction. Maven was including an Oracle jar, version 10.2.0.1.0, and WebSphere had the same jar, but version 10.2.0.4.0.
After fixing the POM so that the 10.2.0.1.0 version jar didn't get deployed, the problem seems to be resolved.

Related

How can I connect to oracle with jdbc from python?

Seeing the last part of this article, I'm dying to do the same on my Eclipse.
import jpype
import jaydebeapi
JHOME = jpype.getDefaultJVMPath()
jpype.startJVM(JHOME, '-Djava.class.path=/ojdbc8-full/ojdbc8.jar')
con = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:XXXXXX/XXXXXX#//99.99.99.99:1521/ABC')
cur = con.cursor()
cur.execute('select dummy from dual')
r = cur.fetchall()
print(r[0][0])
cur.close()
con.close()
And these message have appeard on my screen though, to tell the truth I have no knowledge about JAVA.
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.RuntimeExceptionPyRaisable: java.lang.RuntimeException: Class oracle.jdbc.driver.OracleDriver not found
So far, JAVA_HOME environment variable on my computer is the following.
java home
Since I don't know which jdbc driver is appropriate for my environment, I haven't actually done anything, he recommended downloading though.
And I don't know even where I should put that driver I would get later.
If someone tell me what I should do fast of all, I would be so happy! Thanks.

Using SQLServer JDBC Driver in MATLAB

I need to connect to a SQLServer database from inside Matlab.
Caveats:
Buying licenses to Matlab's Database Toolbox is not an option-- I can only use core-Matlab and Java.
I cannot assume anything about the Java paths when Matlab is initialized.
Please don't tell me to use Python. I already know that but the company I work for does not...
Here's what I've done so far...
I've downloaded the SQL Server JDBC Driver from here.
I've created a sandbox directory, contents pictured below...
Here is my Matlab code.
% Connection params
server = 'myServerName';
port = 1433;
dbname = 'myDatabase';
user = 'user1';
pass = 'password';
url = sprintf('jdbc:sqlserver://%s:%d;DatabaseName=%s',server,port,dbname);
% Importing Java libraries
import java.sql.*;
import java.sql.DriverManager;
import java.sql.DriverManager.*;
% Add the Microsoft SQL Server JAR to Java path
javaaddpath('<path_to_this_folder>\sqljdbc41.jar'); %for JRE7
import com.microsoft.sqlserver.jdbc.*
% Try to instantiate the JDBC Driver
% This way DOES work
% (https://stackoverflow.com/questions/24438359/connecting-matlab-and-mysql-with-the-jdbc-driver)
if true
d = com.microsoft.sqlserver.jdbc.SQLServerDriver;
urlValid = d.acceptsURL(url);
props = java.util.Properties;
props.put('user',user);
props.put('password',pass);
con = d.connect(url, props);
% This way DOES NOT work.
%I referred to the sample code included with the JDBC to write this.
else
% Matlab Doc says this is similar to calling...
% Class.forName(...)
javaObjectEDT('com.microsoft.sqlserver.jdbc.SQLServerDriver');
driver = DriverManager.getDriver(url);
con = DriverManager.getConnection(url, user, pass);
end
It appears that for whatever reason, the DeviceManager is not loading the driver properly. The method DriverManager.getDriver(url) is throwing an Exception...
Java exception occurred:
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
Is there a way to fix this or should I just roll with connecting directly through the Driver itself? I can't think of a reason why it matters. Will there be any consequences? Should I go with a DataSource approach instead?
Thanks!
You might want to try adding the driver to the static Java path rather than the dynamic Java path. Whenever I’ve worked with database drivers, they seem to have trouble if they’re on the dynamic path; I’m not sure why exactly, but they seem to need loading in a particular way that doesn’t work nicely if they’re on the dynamic path.

Using class.ForName to load ucanaccess in a maven project

Using class.ForName to load ucanaccess in a maven project
OK so I am now totally out of my depth. Everything was going so well before I started to use Maven and now its so much more complicated.
Anyway I am trying to connect to a database using ucanaccess.
public Statement ConnectorNoInsert(String HospNum,String SName,String FName,String DOB) throws SQLException{
Preferences userPrefs = Preferences.userNodeForPackage(main.java.Console.TBB_SQLBuilder.class);
String connectDB ="jdbc:ucanaccess://"+userPrefs.get("PathForDB", null);
System.out.println("Connection To Database Made "+userPrefs.get("PathForDB", null));
Connection conn=DriverManager.getConnection(connectDB);
Statement st =conn.createStatement();
return st;
The error that I get is:
ERROR: java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://PhysJava/Physiology.mdb
so I added Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); above the Connection conn line. This gives me the error:
unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown and the project doesnt compile
I suppose the question is: how to call Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); in a maven project. If I need to use a ClassLoader could someone please show me how
The first one ("No suitable driver") is a run-time error, the second one ("unreported exception") a compile-time one. The error message is pretty explicit: "...must be caught or declared to be thrown..." Fix the code in this sense.

The method getConnection(String, String, String) is undefined for the type DriverManager

Here is my piece of code--
String userName = "username";
String password = "password";
String url = "jdbc:sqlserver://WEBDBSRV/DEVPORTAL;test";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url, userName, password);
I am trying to connect my database in one of my java product but getting following error
The method getConnection(String, String, String) is undefined for the
type DriverManager.
The fact that you did not encounter ClassNotFound exception means that DriverManager is in your classpath. The problem might be one of these things:
wrong DriverManger imported
DriverManager class version used during deployment different than used during development
Some clash between two classes called DriverManger and loaded by different class loaders
Get your jar which contains DriverManager. Unpack it and decompile to see the signatures of methods. The fact that you managed to compile the project means that the version you are using during compilation is probably not the one used during deployment.

Issues with Class.forName in Java code using Eclipse IDE

I am using Eclipse IDE Version: Helios Service Release 2 and JDK version 1.6. I have SQL Server 2008 installed on my system. I have downloaded Microsoft JDBC driver and included the path of the jar file in Eclipse IDE-> Project Properties->Java build Path-> Libraries -> Add External jars.
I have written this piece of code for database connection:
package com.ucs.test;
import java.sql.*;
public class ConnectDatabase {
Connection DBconnection = null;
String dbName = "silkopenview";
String userName = "SilkTestAdmin";
String password = "Nbv12345";
Class.forName(drivername);
DBconnection = DriverManager.getConnection(dbName,userName,password);
}
But I get the following errors:
Syntax error on token "DBconnection", VariableDeclaratorId expected after this token
Syntax error on token "drivername", VariableDeclaratorId expected after this token
Syntax error on token(s), misplaced construct(s)
I am new to Java and Eclipse IDE. Please help me in correcting this errors. A quick help is appreciated.
These statements:
Class.forName(drivername);
DBconnection = DriverManager.getConnection(dbName,userName,password);
are currently just part of the class - not in a method, or constructor, or static initializer etc. You probably want to put them in a constructor. The previous ones are okay, as they're variable declarations - although whether you really want them to be instance variables is a different matter.
Also note that driverName isn't declared anywhere in the code you've given.
On a tangential note, if you're sufficiently new to Java that you're running into this sort of thing, you should abandon your current code completely: you're currently trying to run before you can walk. Talking to databases correctly is non-trivial, and trying to learn how to do that while also learning Java syntax is going to be messy. Start with simple console apps that let you learn the language and some of the core types (strings, numbers, collections etc) and then move on to databases.
Class.forName(drivername);
DBconnection = DriverManager.getConnection(dbName,userName,password);
You can not put them where you placed in your class, You have to put them in constructor/method, like :
public class ConnectDatabase {
Connection dbConnection = null;
String dbName = "silkopenview";
String userName = "SilkTestAdmin";
String password = "Nbv12345";
public Connection getConnection() {
Class.forName(drivername);
dbConnection = DriverManager.getConnection(dbName,userName,password);
return dbConnection;
}
}
You need to place your statements in a method rather than in the class block.
Class.forName(drivername);
and
DBconnection = DriverManager.getConnection(dbName,userName,password);
Given that you're using SQL Server, you will need to declare your driverName:
final String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

Categories

Resources