Java/MySQL on Linux
(last updated: Jun 5, 2008)

Select font size:
This document assumes that you've done both the Java Netbeans/JDK installation and the MySQL installation and setup. The test program below assumes that MySQL test database is accessible for the guest user with empty password. The only software you need is the MySQL Connector/J driver JAR file available from the course web site through this link
mysql-connector-java-5.1.6.zip
This package is also available from the MySQL site at:
http://www.mysql.com/products/connector/j/
Unzipping mysql-connector-java-5.1.6.zip yields this directory:
mysql-connector-java-5.1.6
The only file you really need from this directory is
mysql-connector-java-5.1.6-bin.jar
I suggest that you move and rename this file to a version-free, world-readable form such as
/usr/local/share/java/mysql-connector-java.jar

Test the installation

The TestMysql.java program simply loads the driver and connects to the MySQL test database as the guest user. The execution should give an OK on loading the driver, and a second on the connection.

TestMysql.java
public class TestMysql { public static void main(String args[]) { try { /* Test loading driver */ String driver = "com.mysql.jdbc.Driver"; System.out.println( "=> loading driver:" ); Class.forName( driver ); System.out.println( "OK" ); /* Test the connection */ String url = "jdbc:mysql://localhost/test"; System.out.println( "=> connecting:" ); java.sql.DriverManager.getConnection( url, "guest", "" ); System.out.println( "OK" ); } catch( Exception x ) { x.printStackTrace(); } } }

In NetBeans

The key concept in executing a project like this, as opposed to a simple "Hello World" project, is that the execution relies upon the external MySQL JAR file which must be registered in the project's CLASSPATH.
  1. Select File New Project. In the New Project window, select the Java category, and choose Java Application, then click Next.
  2. Enter the project name TestMysql. Uncheck the box Create Main Class. Leave Set As Main Project checked. Click Finish.
  3. In the Projects window, click on Source Packages to reveal the <default package>
  4. Right-click on <default package> and create a new Java Class. Name it TestMysql.
  5. Replace the starter contents of TestMysql.java file by the above file.
  6. Right-click the project name and select Properties.
  7. From the Project Properties dialog, select Libraries and from that the Compile tab.
  8. Click the Add JAR/Folder button and navigate to /usr/local/share/java/mysql-connector-java.jar. Click OK
  9. Run by selecting Run Run Main Project.

In the shell

In order to have the Java executables automatically use this driver package you need to set the CLASSPATH environment variable. You can set it directly in a shell using the command:
export CLASSPATH=.:/usr/local/share/java/mysql-connector-java.jar:
The trailing ":" and the "." (current directory) are both important. Install this setting permanently by adding this export line to the ~/.bash_profile startup file. Verify the correct CLASSPATH value by typing in a shell:
echo $CLASSPATH
Assuming the PATH and CLASSPATH are both correct, run:
javac TestMysql.java
java TestMysql.java

Troubleshooting

  1. If the driver fails to load, double-check the value of CLASSPATH.
  2. Connection failure means that MySQL is not running or something is wrong with the database setup.


© Robert M. Kline