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?
You seem to be working on a linux box. Make sure that your terminal launches a
login
shell. For example thegnome-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.But i would argue against doing this and doing a simple launch script for your application. Something similar to this:
make executable and use that to launch your application.
Try to do :
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.
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 :
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:
Hope this helps
For MAVENS project solution. You can directly modify the pom.xml file. Add the mysql-connector dependency to the pom.xml project file:
Forget the
CLASSPATH
environment variable. It's a big joke. It's ignored by pretty much everything else than a low-leveljava 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 inTomcat/lib
folder (if you use the container managedDataSource
approach for a fast connection pool), or in webapp'sWEB-INF/lib
folder (if you use the poor man'sClass#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 ofjava
command. To save yourself from typing it again and again when executing it, just create a.sh
file with the command.