CS 281 – Fall 2009 Homework #4 Due: 16.11.2009, Monday, till 17

advertisement

CS 281 – Fall 2009

Homework #4

Due: 16.11.2009, Monday, till 17:00

In this homework, you will write a simple Java application to connect and interact with an MS Access database. You will use the lab3.java file that we have in Lab3. You will use the JCreator editor provided at the BCC labs. The program will first connect to the book database (available in the course web site), and then perform several SQL statements. The steps are explained in more detail in the below.

1) Change the name of the java file from “lab3.java”to surname_name_hw4.java

(Do not use Turkish characters). You have to change the java class name in the file as well (from “public class lab3” to

“public class surname_name_hw4 ”). Put it to your personal “My Documents” folder.

2) This code successfully connects the book database using JDBC. As you remember from the Lab3, the location of the book database must be C:\temp folder . Do not change this location in the code.

This code already prints all the authors in the Author table and then adds a new author named as

“Bruce Croft”. Next, you will add the following queries into the code in the given order: a) Execute the command “SELECT * FROM Author” and print the results so that we can see that author “Bruce Croft” is successfully added. b)

Write a query to update the address of the author with ID 777777 (which is the author

“Bruce Croft”) to “University of Massachusetts Amherst ”

c) Execute the command “SELECT * FROM Author” and print the results but this time also print the address field so that we can see that author the address of the “Bruce Croft” is successfully changed. d) Write a delete query to delete all authors who do not have a book yet in our database (which means the authors whose SSN value is not listed in Authorship table) and whose birthrate is later than 1/1/1945. e) Execute the command “SELECT * FROM Author” and print the results so that we can see that author “Bruce Croft” is deleted from the database. f) Write a CREATE TABLE sql statement to add BookAwards table to our book database. This table will store the awards given for a book. BookAwards table will have attributes BAID

(integer), award_name (String), award_date (date) and ISBN (String). Note that you should set

BAID as primary key. g) Next, insert into the BookAwards relation the following records. To achieve this, you can use either Statement or PreparedStatement object of Java.

(111, Best Seller 2003, 31/12/2003, ENG-111111)

(222, Book of the Year 2004, 31/12/2004, ENG-222222)

(333, Best Seller 2005, 31/12/2005, GR-111111) h) Execute the command “SELECT * FROM BookAwards” and print the results. i)

Execute the command “DROP TABLE BookAwards” so that this table is deleted from the database.

While doing the above tasks, you should keep in mind to catch the potential SQL exceptions. You should write appropriate error messages in the catch clauses.

What to submit?

You will upload your java source file to the Moodle page for this hw. Note that, the homework will be graded based on its execution.

Download