You are on page 1of 4

PDC Lab: JDBC

Download the .zip file from AUTOnline. Extract the file, and load project into NetBeans.

Task 1: Get familiar with Java DB by using NetBeans

1.1 Register Java DB


To verify whether Java DB is registered in the IDE:
(1) Open the Services window.

(2) Right-click the Databases > Java DB node and choose Properties.
If Java DB is registered, the Java DB Installation and Database Location fields will be
filled in. If Java DB is not registered, fill in the following two fields:
o Java DB Installation. Enter the path to the database server.

o Database Location. Enter the folder where you want the databases to be stored.
NOTE: please create a DB folder in the H drive or your USB drive. Through this
way, you can access the generated data in other computers.

(3) Click OK

1.2 Starting the Server and Creating a Database


Once Java DB is registered with the IDE, you can easily start and stop the database server, as
well as create a new database.

To start the database server in the IDE:


In the Services window, right-click Databases > Java DB and choose Start Server.
1
Once the server is started, Java DB Database Process tab opens in the Output window and
displays a message similar the following:

Apache Derby Network Server - 10.2.2.0 - (485682) started and ready


to accept connections on port 1527 at 2007-09-05 10:26:25.424 GMT

To create the database:


(1) In the Services window, right-click Databases > Java DB and choose Create Database.
(2) For the Database Name text field, type BookStoreDB. Also set the User Name and
Password to pdc (or whatever you want).
(3) Click OK.

1.3 Connecting to the Database


So far, we have successfully started the database server and created a database. However, we
still need to connect to the new database before we can start working with it in the IDE. To
connect to the BookStoreDB database:
(1) Switch to the Services window (Ctrl+5) and expand the Databases node to see your
new database.
(2) Right-click the database connection node (jdbc:derby://localhost:1527/BookStoreDB
[pdc on PDC]) and choose Connect.

The connection node icon should now appear whole ( ), which signifies that the
connection was successful.

(3) Expand the connection node, right-click its Tables subnode, and choose Execute
Command.
(4) Create the table (Book) showed in Table 1 by using the following SQL statement:

CREATE TABLE BOOK (BOOKID INT, TITLE VARCHAR(50),


CATEGORY VARCHAR(20), PRICE FLOAT);

(5) Click the Run SQL button ( ) in the toolbar of the Source Editor to run the
script.
(6) Insert the 8 recodes in Table 1 by using the following SQL statement:

INSERT INTO BOOK VALUES (1, 'Slum Dog Millionaire', 'Fiction', 19.90),
(2, 'Run Mummy Run', 'Fiction', 28.00),
(3, 'The Land of Painted Caves', 'Fiction', 15.40),
(4, 'Cook with Jamie', 'Non-fiction', 55.20),
(5, 'Far Eastern Odyssey', 'Non-fiction', 24.90),
(6, 'Open', 'Non-fiction', 33.60),
(7, 'Big Java', 'Textbook', 55.00),
(8, 'Financial Accounting', 'Textbook', 24.80);

2
(7) Click the Run SQL button in the toolbar of the Source Editor to run the script.
Table 1: Book

BookID Title Category Price


1 Slum Dog Millionaire Fiction 19.90
2 Run Mummy Run Fiction 28.00
3 The Land of Painted Caves Fiction 15.40
4 Cook with Jamie Non-fiction 55.20
5 Far Eastern Odyssey Non-fiction 24.90
6 Open Non-fiction 33.60
7 Big Java Textbook 60.00
8 Financial Accounting Textbook 24.80

Task 2: JDBC Programming


Open the bookstore package in your project. You will find that there are 2 classes in the
project. Add code to these 2 classes to achieve the following things:

2.1 Connect to Java DB


Add code in the BookStore.java -> connectBookStoreDB() to establish a connection to the
BookStoreDB database that you created in Task 1. The url of your Java DB host should be:
jdbc:derby://localhost:1527/BookStoreDB. You can also find the url by checking the
properties of BookStoreDB (Right-click jdbc:derby://.../BookStoreDB and choose
Properties.).

You also need to include the driver (derbyclient.jar) of JaveDB into the project libraries. The
driver can be found in the folder where Java was installed.

2.2 Create a new table


Add code in BookStore.java -> createPromotionTable() to create a new table called
Promotion (Table 2) in BookStoreDB. Insert the records in Table 2 into the Promotion table.
3
Table 2: Promotion
Category Discount
Fiction 0
Non-fiction 10
Textbook 30

2.3 Get query results


Add code in BookStore.java -> getWeekSpecial() to query Price and Discount from the Book
table and the Promotion table, respectively. Hold the query result in a ResultSet object (rs),
and return the object.

2.4 Store query results into a new table


Add code in BookStore.java -> createWeekSpecialTable() to store the query results obtained
from 3 to a new table called WeekSpecial (Table 3). To achieve this, you need to:

 Create the WeekSpecial table (Table 3)


 Pass the ResultSet returned from getWeekSpecial() to createWeekSpecialTable()
 Calculate the special prices of all the books based on the results in rs. Discount in
Table 2 specifies how much discount (e.g., 10%, 30%) you can give for each
category. For example, according to the values in Table 1 and Table 2, we can find
that the special price (after discount) for “Big Java” should be 60*(1-30%)=42.
 Insert titles and special prices of all the books into the WeekSpecial table.

Table 3: WeekSpecial

Title SpecialPrice

Task 3: Delete existing tables before creating


If you run your program more than once, you may have found that it cannot run properly as
there are existing tables in the database. What you can do is: manually delete the existing
table which has the same name with the new table (in JavaDB). There is a “DROP TABLE
IF EXISTS TableName;” command in MySQL, which can check and delete existing tables.
Unfortunately, this statement is not supported by JavaDB. So can you write some code to
realise this function in your program. You can include the code before creating new tables, so
that the program will automatically detect and delete existing tables which have the same
names with the new tables.

Hints: you may need some methods in DatabaseMetaData in this task

You might also like