Computer Science 396S
Assignment 4
Due: October 8, 2003

For your fourth assignment, you are to create a database and write a Java program that connects to it.  You will have to register the database with the Windows operating system.  Follow these instructions:
    In Windows 98 click on Settings/Control Panel/Data Sources (ODBC).  In Windows 2000 or XP, the same file is in Settings/Control Panel/Administrative Tools.  Select Add/Microsoft Access Driver (*.mdb), and from there Browse to find the location of the database.  This works with databases on a floppy disk as well as on the hard drive.

You may store anything you want in the database, however you may find it easier to continue with the data you used for assignment 3.  In Access first create the database.  You can do this in Design View.  Select four or five fields for the database.  The names should start with upper case letters and contain no spaces.  (This is not really necessary but it will make things easier.)

Save the database either on your hard drive or on a floppy.  If you do not have Access, we will register a database called “products” on one of the computers in room 201 of 163 William Street.  Let me know if you will have to use this computer rather than your own machine.

Then write a Java program that will list all the items in the database.  You can follow the Addresses program on the other side or the one below.

// Fruit connects to a database and displays its contents on the screen.
import java.sql.*;
public class Fruit
{
     public static void main (String [] args) throws Exception
     {
          Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection con = DriverManager.getConnection ("jdbc:odbc:fruit");
          Statement stmt = con.createStatement ();
          String query = "SELECT * FROM FruitTable";

          ResultSet rs = stmt.executeQuery (query);
          System.out.println ("Fruit Table Results");
          while (rs.next ())
          {
               System.out.println (rs.getString ("ID") + " ");
               System.out.println (rs.getString ("Name") + " ");
               System.out.println (rs.getString ("Variety") + " ");
               System.out.println (rs.getString ("PricePerPound") + " ");
               System.out.println ();
          }
          con.close ();
     }
} // Fruit