Professional Documents
Culture Documents
Lecture # 13
Outline
Database and DBMS
Relational Databases
JDBC
What Does JDBC Do?
Case Study: The Books Database
The DBConnect Class
Login Database Example
Store Database Example
2
Database and DBMS
3
Relational Databases
Most popular DBMSs are relational databases
– A relational database stores data in tables
– Tables are composed of rows, and rows are composed of columns
in which values are stored
– Primary key: a column (or group of columns) in a table with a
unique value that cannot be duplicated in other rows
SQL (sequel) is the international standard language used
with relational databases to perform queries
Some popular RDBMSs: Microsoft Access, Microsoft SQL
Server, Oracle, and MySQL
In this course, we will use Microsoft Access
4
JDBC
5
What Does JDBC Do?
6
Case Study: The Books Database
8
Case Study: The Books Database
publishers table
– publisherID
An autoincremented integer (primary-key)
– publisherName
The name of the publisher (a string)
9
Case Study: The Books Database
titles table
– Isbn
ISBN number of the book (a string)
– Title
Title of the book (a string)
– editionNumber
Edition number of the book (an integer)
– Copyright
Copyright year of the book (a string)
– publisherID
Publisher’s ID number (an integer). This value must correspond to
an ID number in the publishers table (Foreign Key).
– imageFile
Name of the file containing the book’s cover image (a string)
– Price
Suggested retail price of the book (a real number)
10
Case Study: The Books Database
11
Case Study: The Books Database
authorISBN table
– authorID
The author’s
ID number,
which allows
the database
to associate
each book with
a specific
author.
(foreign key)
– isbn
The ISBN
number for a
book (a string),
foreign key
12
The Books Database: Console Example
An Example Program
standard output.
13
The Books Database: Console Example
import java.sql.Statement;
import java.sql.ResultSet;
public class AuthorManager {
15
The Books Database: Console Example
public static void main(String[] args)
{
AuthorManager manager = new AuthorManager();
manager.viewAuthors();
Another Example
17
Login Database: Console Example
import java.sql.*;
public class LoginManager {
public static void main(String[] args) {
boolean validUser = false;
ConsoleReader myConsole = new ConsoleReader(System.in);
18
Login Database: Console Example
DBConnect dbConnectObj = new DBConnect();
dbConnectObj.getDBConnection("LoginDB.mdb");
Statement statement = dbConnectObj.getStatement();
try{
ResultSet rs = statement.executeQuery(query);
if(rs.next()){
validUser = true;
} //end while
}//end of try
catch(Exception exp){
validUser = false;
System.out.println("Exception: "+exp.toString());
}//end of catch
finally{
dbConnectObj.disconnectDB();
}
19
Login Database: Console Example
if(validUser){
System.out.println("You are a member. ");
}//end if
else{
System.out.println("You are not a member. ");
}//end else
}//end of main
}//end of the class Enter Login: abc
Enter Password: abc
---------------
Requesting Connection...
Connection Established...
---------------
---------------
Connection Closed
---------------
You are a member. 20
Store Database: Console Example
Example
– A program which establishes connection with the
store database using DBConnect class, sends a select
query to the Item table, and displays the results of
query on the standard output.
– The output from this program includes column
headings of Item table.
21
Store Database: Console Example
import java.sql.*;
class ItemManager {
public static void main(String[] args) {
DBConnect dbconnect = new DBConnect();
dbconnect.getDBConnection("StoreDB.mdb");
Statement statement = dbconnect.getStatement();
String query = "SELECT * FROM ItemTable";
try{
ResultSet results = statement.executeQuery(query);
ResultSetMetaData rsmetadata = results.getMetaData();
int colcount = rsmetadata.getColumnCount();
for(int i=1; i<=colcount; i++)
{/*displaying col. names*/
System.out.print(rsmetadata.getColumnName(i)+"\t");
}
System.out.println();
22
Store Database: Console Example
while(results.next()){
for(int i=1; i<=rsmetadata.getColumnCount();i++){
System.out.print(results.getString(i)+"\t");
}
System.out.println();
}
}//end of try
catch(SQLException e) {
/*statement.executeQuery method throws java.sql.SQLException*/
System.out.println(e);
e.printStackTrace();
}
finally{ dbconnect.disconnectDB(); }
}
}
23
Store Database: Console Example
---------------
Requesting Connection...
Connection Established...
---------------
ItemID Name NoOfItems UnitPrice Description ImgPath
G100 T-Shirt 110 275.5 dfgh c:\images\tshirt.jpg
G101 Shirt 50 599.99 dfgh dfgh c:\images\shirt.jpg
S200 Black Shoes 15 1500.0 hjkl jkl kl c:\images\shoe.jpg
25