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
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 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've been using products around Hadoop, but new to developing an application with them using Java.
I'd like to use some classes like HiveStatement, but found that there are the same name classes with different packages.
ex) org.apache.hive.jdbc.HiveStatement, org.apache.hadoop.hive.jdbc.HiveStatement.
I noticed this when trying to cast a Statement object into HiveStatement. I was trying to cast a object into org.apache.hadoop.hive.jdbc.HiveStatement, but casting into org.apache.hive.jdbc.HiveStatement seems right in my case.
What's the difference, and why do two similar packages exist?
Thanks in advance!
org.apache.hadoop.hive is the old Java package name for Apache Hive, for example version 0.8.x of the Hive JDBC driver.
Around the 1.0.0 release time frame, the package name changed.
You should use org.apache.hive for newer versions.
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
I'm assuming I need to use Connector\J and JDBC to achieve this, but I can't seem to 'install' Connector\J using the CLASSPATH thing. How do I do that ? I use the IntelliJ IDE if thats relevant.
I'm looking for a way to talk to a mysql database and execute and print out a few basic queries but I'm not getting anywhere because I can't even talk to the database.
Any help is appreciated.
You might follow an example such as this one:
http://www.kitebird.com/articles/jdbc.html
But, you might want to consider using Hibernate unless you're doing only a couple basic queries.
See Using MySQL with Java -- the first link from Google.
I like this tutorial because it's very detailed and thorough. Pretty much every step from downloading and installing MySQL and Connector/J to writing and running queries is covered.
Which version of IntelliJ?
You need to add the MySQL Connector-J JAR to your project dependencies.
Open your IntelliJ settings (the "wrench" in the top menu bar), go to "Libraries", click on "Attach Classes", and add the MySQL Connector-J JAR that you downloaded above. That puts it in your CLASSPATH.
If you still have problems, it means you don't have the driver class name right. It should be com.mysql.jdbc.Driver.