I wrote some code that connects to a PostgreSQL 9.4 DB via its latest JDBC4 driver. My understanding is that JDBC4 no longer needs the boilerplate "Class.forName("org.postgresql.Driver") driver registration line. Thus, I left it out. It worked fine.
I then put the same exact code in a servlet, added the postgreSQL JDBC jar to WebContent/WEB-INF/lib and it failed saying:
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/
When I add the Class.forName line, it then works fine.
Does anyone know why this JDBC4 driver needs Class.forName when put within a doGet() servlet method, but in a basic Java class it doesn't?
Thanks,
TR
edit: Forgot to mention, I'm using Java 7.
Some updates:
It does appear that this is a fairly common issue in Tomcat. I've moved the JAR out of WEB-INF/lib per the suggestions from this, and other posts. Even with it in TOMCAT_HOME/lib I need to use Class.forName(). Sort of odd, I suppose it has something to do with the way Tomcat registers classes.
Found this post which is the exact issue with the mySQL JDBC driver rather than postgreSQL:
When is Class.forName needed when connecting to a database via JDBC in a web app?
Thanks all for the help with this. I found the exact situation and answer in the Tomcat 7 documentation while learning how to setup a JNDI datasource:
http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html#DriverManager,_the_service_provider_mechanism_and_memory_leaks
In short, under these circumstances, you need to register the driver using Class.forName even if you are using JDBC4 on Tomcat 7.
Thanks everyone for pointing me in the right direction!
TR
Related
The question that I will ask does not contain any exception or anything. That's where the confusion coming from.
I have been migrating one old legacy application -which connects and writes to Database- to java8. It used to use the db2 library along with the license from /home/db2client/v95fp2/sqllib/java/db2jcc_license_cu.jar:/home/db2client/v95fp2/sqllib/java/db2jcc.jar. And when I check the details of the db2jcc.jar it says IBM DB2 JDBC Universal Driver Architecture.
Now, deleted that library from the classpath and added my new jar under the lib directory of the project db2jcc4-10.5fp11-fp11.jar and deleted the db2jcc_license_cu.jar And when I check the details of the new jar it says IBM Data Server Driver for JDBC and SQLJ. (The reason I changed to the new jar was there was some java8 combability problems in between the old one and java8)
Eventhough I do not have the license and the jars are different, my application still manages to run and write to the database without an exception. This is the reason I am very confused at the moment.
What is the difference between Universal Driver Architecture one and the Data Server Driver for JDBC SQLJ? And why is it still able to run? I have been reading the whole IBM manuals but I still do not know.
I am aware it may not be a type of question that is asked here, but I think that'd help me a lot to understand what's happening.
I am getting below exception while trying to insert records into database using CallableStatement.
Total records : 1300
To due to some security reasons not able to provide code snippet sorry for that but its normal CallableStatement executing perfectly for other flow in my project.
Throwing exception at 1327. stmt.executeBatch(). stmt is object of CallableStatement.
An ArrayIndexOutofBoundsException could be thrown by the Oracle JDBC driver.
Here you will get exact details of error from oracle.
It happens if the batch is too big. So either set the batch smaller in the OracleOutput component or try a different mode for inserting the data.
In order to resolve the issue, patch for BUG 6396242 must be downloaded from Oracle's support site and applied prior to using it.
You can resolve this problem by downloading the new version of ojdbc of 11 or greater version from OJDBC.
If you see this read me file here BUG-6396242, it has been fixed in Oracle JDBC Drivers release 11.1.0.7.0 - Production version.
Here i had attached screenshot of same.
Your problem could be related with the Oracle JDBC driver #6396242 bug: ArrayIndexOutOfBoundsException: -32nnn.
Please, try to download a patch for the bug from Oracle Support.
If you do not have access to Oracle Support, you can also download a recent version of the Oracle JDBC driver for your database version from the following link, it will also fix the problem.
This is a known issue in the mysql driver it has been fixed in 5.1.13 more details [here]
1, so to fix your bug simply upgrade it to the latest version.
You've got a bug. It is in your code.
Go to file com.mypackage.accounts.server.util.UtilClass.
Go to line 1356.
On it, you will find at least 1 array access (i.e. foo[x]). The number inside the []? It is -32593 for some reason. If there are more than 1 [] in that line, it's one of them, not possible to tell which one from this stacktrace.
Without you pasting the code we can't tell you why. I can tell you that PreparedStatement has nothing whatsoever to do with any of this.
I am migrating a legacy project to a new server. Previously the project used a Oracle DB but now i want it to use Postgress. The queries are simple enough and work the same in Postgres.
However the project is missing a Postgres jdbc-driver. Can i somehow add this dependency sideways to the jar without recompiling?
Can i somehow add this dependency to the jar without recompiling?
It depends.
If you are running the server as java -jar myserver.jar ..., then you will at least need to modify the manifest in the JAR file. Strictly speaking this doesn't entail recompiling, but you do need to explode, modify and repack the JAR file.
If the server uses Class.forName to explicitly load an Oracle Driver class, then you will need to change that code to load the Postgres Driver class instead. (There are other ways to use JDBC that avoid this, but this depends on how your legacy server is implemented.)
If your server uses Oracle specific database classes, or Oracle specific SQL features (or it needs to do the same in the Postgres world) then more extensive changes will be required.
But without actually examining your codebase in detail, we can't predict what is required.
My advice is to replace the Oracle driver JAR with a Postgres driver JAR, and see what happens when you run your server against a Postgres database with the appropriate schemas and data.
But I wouldn't do this "in production". Do it in a test environment. If you can't set up a suitable test environment ... forget it.
And if you don't have the source code for your server, I would forget it too. If anything goes wrong you will most likely need source code to figure out the problem and fix it.
I have multiple applications that needs to connect to MSSQL using windows authentication.
The First webApp which is loaded works fine. but the remaining fail prompting
Caused by: java.lang.UnsatisfiedLinkError: Native Library $tomcat/bin/sqljdbc_auth.dll already loaded in another classloader
Below is the code used
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection= DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=XXX;integratedSecurity=true");
I Have placed
sqljdbc*.jar --> tomcat*/lib and sqljdbc_auth.dll --> tomcat*/bin
Seems like all my applications are trying to load the shared lib($tomcat/bin/dll) multiple times. Hence the first load works and the remaining fail.
Edit: I understand that the native library (DLL) can only be loaded into the JVM once, hence the error, but I after looking around the net I still have no solution.
How can i load the dll only once?
Please Help!!
I would guess. That removing the line
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
would solve the problem.
As mentioned in the javadoc of DriverManager
The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism.
...
Applications no longer need to explictly load JDBC drivers using Class.forName().
edit This requires a jdbc driver which supports the JDBC 4.0 API. Which should be the case from Microsoft JDBC driver 4.0 on (see: https://learn.microsoft.com/en-us/sql/connect/jdbc/system-requirements-for-the-jdbc-driver)
A matrix which Microsoft JDBC driver supports which SQL server version you can find at https://learn.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sql-server-support-matrix
I am developing an app in Java with Netbeans 6.9.1 as the IDE. I use a MySQL database, and connect to it using JDBC.
I have a curious problem.
Class.forName() does not throw ClassNotFoundException in one package, whereas it throws it in another. What might be wrong?
JDK version is 1.6 and I am using Ubuntu 10.10.
Class.forName() does not throw
ClassNotFoundException in one package,
whereas it throws it in another. What
might be wrong?
The only two scenarios I can think of for this are:
One package is an application package and the other is a test package. In this case, you'd have to make sure Connector/J is added to the regular library list and the test library list as well.
They're two different projects, at which rate you'd have to add Connector/J to both projects.
If you use to connect mysql with netbeans, you may have to do the steps in the following link.
----->Netbeans Help