Computer Science 241
Assignment 1
Due:
This semester we are going to
work on software for a library. Your
first assignment will be to create a database with either a table for books or
one for borrowers. You may use either
Microsoft Access of MySQL. Follow the
instructions on the handout on creating an Access database.
One of the fields should be
an ID. For books, that will be the ISBN,
the International Standard Book Number. For
borrowers, create an ID. We no longer
use social security numbers for general IDs.
You should consider carefully what the other fields should be. Altogether have four or five fields. You can add other fields later.
When you have created the
database, add some data. Then write a
program in Java that will connect to your database and display the results on
the screen. Follow the example in the
handout or the one listed below.
// Produce connects to a
database and displays its contents on the screen.
import java.sql.*;
public class Produce
{
public static void main (String [] args)
{
try
{
// Open a connection to the database.
Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con = DriverManager.getConnection ("jdbc:odbc:produce");
// Create
a statement object and use it to query the database.
Statement
stmt = con.createStatement ();
String
query = "Select * From ProduceTable";
// Get the
results from the query and display them on the screen.
ResultSet
rs = stmt.executeQuery (query);
System.out.println
("Produce Table Results");
while
(rs.next ())
{
System.out.println
(rs.getString ("ID")); // The
datatype of these fields is Text.
System.out.println
(rs.getString ("Type"));
System.out.println
(rs.getString ("Name"));
System.out.println
(rs.getString ("Variety"));
// The datatype of the Price field is
Currency.
System.out.println
(rs.getDouble ("Price"));
System.out.println
();
}
con.close
();
} catch (ClassNotFoundException
e){System.out.println
("Class Not Found exception.\n");}
catch (SQLException e){System.out.println ("SQL
Exception\n");}
} // main
} // Produce
A picture of the database
table.