This week's book giveaways are in the Angular and TypeScript and Web Services forums.
We're giving away four copies each of Programming with Types and The Design of Web APIs and have the authors on-line!
See this thread and this one for details.
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Add JDBC driver to classpath?

 
Ranch Hand
Posts: 92
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How to add jar file containing the JDBC driver on the classpath, to resolve issue below?
3a.png
[Thumbnail for 3a.png]
 
Greenhorn
Posts: 1
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better way to resolve all the  dependencies related issue is use maven projects and then add all the jar from mvnrepository.com
 
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SK: welcome to the Ranch
 
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three typical cases...

(1) Best case: Maven Project and public Maven dependency exists:

If you have a Maven project AND there is a Maven dependency available online the absolute best way is to add the JDBC dependency to your pom.xml.


(2) Regular Java Project


If you have a regular (non-maven) Java project and you have downloaded the JDBC JAR library, you add that file to your library area in the IDE (say in Intellij, or Eclipse or the build path ("Add External Jar" in Eclipse)).


(3) Maven project, but no public maven dependency found:


Now, if you have a Maven project, but the JDBC library is not available in a Maven repo, then you need to add that JDBC jar to a local Maven repository you create. Then you use your own local maven dependency to add that jar file to your pom.xml so the JDBC library will be in your built JAR or WAR file. If you don't take this step, the project will work OK when you run it from the IDE, but without the actual Maven repository element in your pom.xml, you will have a ClassNotFound Exception when you try to run the same JDBC from the built Maven JAR or WAR file.

Does that help?

-- mike
 
Saloon Keeper
Posts: 21437
143
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, that's not nearly enough information.

Your error is a screenshot of a dialog. Which implies to me that you're not getting the error from an application, but more probably from some sort of GUI tool or database browser/editor. In which case it's important to know that that program is.

JDBC drivers are typically implemented in JAR files and how they get connected to applications depends on what type of application you are trying to run and how it's constructed. For example, in a web application, you'd normally manually copy the JDBC driver jar to a shared resource (library) folder in the webserver itself (since if you're using JDBC connection pools, the webapp server owns the database connections, not the web applications). An IDE, however, typically has a preferences dialog(s) that can be used to point to a copy of a JDBC jar. For example, the Eclipse database editor plugin running on a Linux machine would be generally be told to point to a jar file somewhere under /usr/share/xxxx, where "xxxx" is the name (and possibly version) of an OS-installed JDBC client package. Like for example     /usr/share/java/mysql-connector-java.jar    or /usr/share/java/postgresql-jdbc.jar. Or, you could just simply yank the jar directly from the database vendor's website and put it anywhere you want, then point the database editor at it.

For stand-alone apps using JDBC, you generally have 2 choices. One is to pass the external file location of the JAR in as a command parameter and have logic in the app that alters its classpath so that classes in that JAR are seen as part of the overall application classpath. The other is to use Maven or a similar tool to build an executable JAR. In such a case you'd specify the JAR as a Maven dependency. Note that a standard Maven build cannot do what you want, since classpaths cannot see JARs that are embedded inside other JARs. The Maven executable JAR mojo adds the extra logic needed to alter the app's internal classpath so that the embedded JAR can be seen by the classloaders.

Actually, you could alternatively explode the JDBC JAR and copy all its files directly into an application's JAR, but that's rather crude and ugly.

 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
What to add to my project?
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we don't know what's in your project, how could we tell you what to add to it?
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What to further add to project, in addition to the following?

 
Rancher
Posts: 4405
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you have a stack trace, I assume you have the full exception text.
That will tell you what class is missing.

You haven't told us what database you are using either.
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stacktrace is shown through the screenshot in above.
It is to connect to SQL server DB.
 
Dave Tolls
Rancher
Posts: 4405
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But a full exception looks like this:
java.lang.ClassNotFoundException: some.class.NotExist
   at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
   etc.


Yours is missing the first part, which tells you what the actual problem is.

So what class is missing?
What is the full exception text?

It''s a good habit to get into.

One more question, do you have the SQL server JDBC driver?
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As written about in the book Debugging the Development Process, a very good habit is to always step through your code with the debugger. Stepping through your code can be very illuminating about what it's doing and, often, how to improve it.

In this case, your code will crash on the line of code that's the offender. You seem to have the line of code (381?) in your error message itself however.

Why don't you post your entire Java program so we can better help you?

-- mike
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
Please help further if possible.
 
Dave Tolls
Rancher
Posts: 4405
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the SQL Server JDBC driver jar file?
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Liang wrote:Mike,
Please help further if possible.



I see in the code you sent me, this:



One problem here is you're closing the connection inside the ResultSet loop so if you have more than one value in your ResultSet, this will crash.

Also, as Dave mentioned above, you'll need to add, if you haven't already, the SQL Sever JDBC JAR file to your project's classpath.

Have you set a breakpoint and stepped through your code?

-- mike

 
Dave Tolls
Rancher
Posts: 4405
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the OP hasn't got the jar file on their runtime classpath then it's never going to get to that line in the first place.

Unless that issue has been sorted, but I can't see a post to that effect.
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:If the OP hasn't got the jar file on their runtime classpath then it's never going to get to that line in the first place.

Unless that issue has been sorted, but I can't see a post to that effect.



Yep, agreed.
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the path to put Jar file? I cannot put sqljdbc4-2.0.jar into path "./usr/share/eclipse/dropins/babel/eclipse/plugins" in CentOs, using root user. Any reason?
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you help?
 
Tim Holloway
Saloon Keeper
Posts: 21437
143
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put the JDBC driver JAR anywhere that the application that's using it can read from. Note that in CentOS, the command:

yum install mysql-connector-java

can be used to make CentOS download the JAR and install it into the system-managed resource set. The command:

rpm -qil  mysql-connector-java

can then be used to see where the JAR file was installed.

Once you have a JDBC JAR you have to tell each application that uses that JAR where it is. For a stand-alone Java app, you'd define that location as part of the application's classpath. For  an executable JAR, you'd have to include the JAR in the JAR building process. For an application like Eclipse, you'd have to tell whatever Eclipse database plugings you have using their configuration dialogs. In some cases that would be part of defining an actual database connection profile.
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I did the setup per the command in above but I still have got error below.
7b.png
[Thumbnail for 7b.png]
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Eclipse, take a look at your configured external JAR build path.

It should look something like the image below (your SQL Server JDBC JAR file is probably a different version).

HTH

-- mike
sqlserver-jar-in-build-path.png
[Thumbnail for sqlserver-jar-in-build-path.png]
SQL Server JDBC JAR file added to Eclipse
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Do I need to add relevant JAR file below?
8e.png
[Thumbnail for 8e.png]
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see your SQL Server JDBC JAR file there so that's a problem.

Using Add External JARs, navigate to your SQL Server JDBC JAR file and add it.

You should be good to go after that and you can work on the rest of your program's coding issues.

HTH

-- mike
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the library shown below but I still got the error (shown below).
9a.png
[Thumbnail for 9a.png]
9c.png
[Thumbnail for 9c.png]
 
Mike London
Bartender
Posts: 1735
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your "doConnect()" method is trying to connect to SQL Server.

All I see in your updated build path are libraries for MySQL.  

(Speaking of MySQL, though, there is no reason to have two MySQL JDBC drivers in your build path (per your screenshot above) as that could cause issues with MySQL connections.)

-----

So, .... You need to add the JDBC driver for SQL Server.

Check this page where you can download it if you don't already have it...

https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15

-- mike
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to enable file like sqljdbc_7.2.2.0_enu.tar, to the project?
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use a utility to "unzip" the ".gz" file.

Then, using the Build Path in Eclipse and "Add External JAR".

-- mike

P.S. The most recent JDBC driver appears to be: "sqljdbc_7.4.1.0_enu.tar.gz"
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is still exception like

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Main.doConnect(Main.java:86)
at Main.main(Main.java:46)


while project is running having mssql-jdbc-7.2.2.jre11 inside.
 
Saloon Keeper
Posts: 10993
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error quite literally tells you what's wrong. Your version of Java only supports class file versions up to version 52.0, but your driver has class file version 55.0.

Class file version 52.0 corresponds with Java 8. Class file version 55.0 corresponds with Java 11.

So either upgrade your JRE to 11+, or downgrade your JDBC driver to Java 8.
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Can you advise what to adjust below, due to the error?
1d.png
[Thumbnail for 1d.png]
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Any help?
 
Saloon Keeper
Posts: 2871
370
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Liang wrote:Hi, Any help?



Did you follow Stephan's recommendations?

Stephan van Hulst wrote:The error quite literally tells you what's wrong. Your version of Java only supports class file versions up to version 52.0, but your driver has class file version 55.0.

Class file version 52.0 corresponds with Java 8. Class file version 55.0 corresponds with Java 11.

So either upgrade your JRE to 11+, or downgrade your JDBC driver to Java 8.

 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed JRE to version 11 below,

But I still get this exception

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/microsoft/sqlserver/jdbc/SQLServerDriver has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Main.doConnect(Main.java:86)
at Main.main(Main.java:46)


2b.png
[Thumbnail for 2b.png]
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please help
 
Mike London
Bartender
Posts: 1735
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Liang wrote:Hi,
Please help



Hi Peter,

You need to use, as previously mentioned above, the "Add External Jars" button. Click it and navigate to your SQL Server JDBC driver (the link to download the drivers also posted above and explained).

Once you add the JAR, you should be all set.

I would recommend you pick up a good book on JDBC or watch some videos demonstrating adding a JDBC driver to SQL Server in Eclipse. Check out YouTube.

HTH

- mike
 
Peter Liang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. How to change against last item "JavaSE 1.8" in the screenshot above?
 
Marshal
Posts: 24820
60
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Liang wrote:I changed JRE to version 11 below,



No, you didn't. Look at the screen shot you posted, and look at the entry which says what JRE you're using. So, again, you need to be using JRE version 11.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!