JDBC CLASSPATH Not Working

2020-04-11 11:37发布

I'm setting up a simple JDBC connection to my working MySQL database on my server. I'm using the Connector-J provided by MySQL. According to their documentation, I'm suppose to create the CLASSPATH variable to point to the directory where the mysql-connector-java-5.0.8-bin.jar is located. I used export set CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH. When I type echo $CLASSPATH to see if it exists, everything seems fine. But then when I open a new terminal and type echo $CLASSPATH it's no longer there. I think this is the main reason why my Java server won't connect to the JDBC, because it isn't saving the CLASSPATH variable I set.

Anyone got suggestions or fixes on how to set up JDBC in the first place?

5条回答
▲ chillily
2楼-- · 2020-04-11 11:52

You seem to be working on a linux box. Make sure that your terminal launches a login shell. For example the gnome-terminal application (probably what you are using if you are using a ubuntu or fedora box for development) has a checkbox under the profile preferences > Title and command menu called "run command as a login shell". Check that and make sure you also put this code in the ~/.bash_profile file at the end.

export CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH

But i would argue against doing this and doing a simple launch script for your application. Something similar to this:

#!/bin/bash

Set CLASSPATH=CLASSPATH=/path/mysql-connector-java-5.0.8-bin.jar:$CLASSPATH

java -cp "$CLASSPATH" <your.main.application.Class>

make executable and use that to launch your application.

查看更多
混吃等死
3楼-- · 2020-04-11 11:54

Try to do :

Class.forName("com.mysql.jdbc.Driver"); 

before getting your connection

You might also want to get a more recent version of the mysql jdbc driver. 5.0.8 is pretty old.

Take a look at JDBC Basics it will give you some tips.

This tutorial might help you as well.

查看更多
孤傲高冷的网名
4楼-- · 2020-04-11 11:59

I know this is an old thread but it might help someone.

I have succeeded connecting to SQLite3 on Windows 7 with the proper CLASSPATH

I use the JDBC driver sqlite-jdbc-3.7.2.

First I tried to copy the driver under the directory c:\Program Files\java\jre7\lib and then set the CLASSPATH variable, but that failed to recognize the line :

Class.forName("org.sqlite.JDBC");

I think that's due to the Windows bug to have spaces in a folder name like "Program Files".

So I copied the JAR file under C:\jbmorla and set the CLASSPATH to:

.;c:\jbmorla\sqlite-jdbc-3.7.2.jar

Hope this helps

查看更多
混吃等死
5楼-- · 2020-04-11 12:02

For MAVENS project solution. You can directly modify the pom.xml file. Add the mysql-connector dependency to the pom.xml project file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
</dependency>
查看更多
男人必须洒脱
6楼-- · 2020-04-11 12:06

Forget the CLASSPATH environment variable. It's a big joke. It's ignored by pretty much everything else than a low-level java com.example.Foo command. It's ignored when you add the -jar argument. It's ignored when you add the -cp or -classpath argument. It's ignored by IDE's and web/application servers. That environment variable was intented as a convenience for starters. But beyond that, it's useless.

It boils down to that the JAR file has got to be placed in any of the existing/default paths of the runtime classpath of the Java application in question, or that at least the path to the JAR file is to be added to the runtime classpath of the Java application in question.

Since you're talking about a server and considering the fact that the CLASSPATH environment variable doesn't work, I'll assume that it's actually a webserver/appserver such as Apache Tomcat. If that's indeed true, you've got to either drop the JAR file in Tomcat/lib folder (if you use the container managed DataSource approach for a fast connection pool), or in webapp's WEB-INF/lib folder (if you use the poor man's Class#forName() approach to load the driver).

If it's not and it's actually a Java application which is to be executed as a JAR, then you've got to specify the classpath in MANIFEST.MF file of the JAR in question. But if it's also not that and it is a loose .class file, then you've got to specify the classpath in -cp or -classpath argument of java command. To save yourself from typing it again and again when executing it, just create a .sh file with the command.

查看更多
登录 后发表回答