JDBC and MySQL

Although servlets and JSP files often connect to databases from a Java program, use of JDBC is independent of Web programming. One can write a stand-alone program that connects to a database. This discussion will describe how to run the QueryViewer example from the text.

  1. Create a directory: QueryViewer.

  2. Download and install MySQL and its JDBC driver.

    1. Go to www.mysql.com/downloads.
    2. Select the latest stable MySQL release and download it. It will be a .zip file.
    3. Unzip it and execute it's setup.exe file. Let it install itself at its default location (in C:\mysql). If you install it anywhere else, you will have to modify one or more configuration files. (See the rather extensive documentation that comes with the download. I once installed it somewhere else, and even though I went though an uninstall process, I couldn't get Windows NT to forget that the previous installation was on my G: drive. To get the new installation to run, I had to move a couple of files to the G: drive.)
    4. Select the latest release of the JDBC driver and download it to your QueryViewer directory. It will be a .jar file.
    5. Add a reference to it to your CLASSPATH. A .jar file functions like a directory, so make sure the .jar file itself is in the CLASSPATH. E.g.,
           set CLASSPATH=%CLASSPATH%;C:\CS320\QueryViewer\mm.mysql-2.0.4-bin.jar.
           
      If you are using JBuilder, include the .jar file as a required library.
  3. Create a QueryViewer/cwp subdirectory and download the QueryViewer's .java files.

    The .java files can be found in abbott.calstatela.edu/courses/cs320b/QueryViewer/cwp. Download them to your own QueryViewer/cwp directory. These files are a revision of the book's QueryViewer (archive.corewebprogramming.com/Chapter22.html) to accommodate MySQL. The original is set up for just Oracle and Sybase.

    The MySQL URL ("jdbc:mysql://" + host + "/" + dbName) and driver ("org.gjt.mm.mysql.Driver") strings are found in DriverUtilities.java.

  4. Compile all the .java files

  5. Start the MySQL server.

    In a Command Prompt window, navigate to C:\mysql\bin. Execute

       mysqld --standalone
    or
       mysqld-nt --standalone.

  6. Start the query viewer.

    From the QueryViewer directory, execute

       java cwp.QueryViewer