Java/MySQL on Windows
(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 form such as
c:\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 c:\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:
set CLASSPATH=.;c:\mysql-connector-java.jar;
The trailing ";" and the "." (current directory) are both important. In modern Windows systems, set CLASSPATH as an environment variable:
Control Panel  System  Advanced  Environment Variables
System (or User) Variables:  New 
   Variable Name: classpath
   Variable Value: .;c:\mysql-connector-java.jar;
Compile and run either from a shell or through TextPad, if you have that installed. Verify the correct CLASSPATH value by typing in a shell:
set 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. Connection failure may also be generated by over-secure Windows firewall settings.
  3. Windows installations have been known to not recognize localhost. This is the so-called "loopback host" with IP number 127.0.0.1, defined in the file c:\windows\system32\drivers\etc\hosts You can check whether there really is a line in this file of the form:
    localhost    127.0.0.1
    


© Robert M. Kline