Java Reference
In-Depth Information
The class PersistentDB implements the DBManager interface. It accesses a
SQL database by means of the JDBC technology (see Sidebar 14.2). There is
only one attribute, connection , that contains the connection string. The
program that instantiates the DB manager is responsible for loading the
appropriate driver.
The class has a set of methods, getItem() , getEmployee() and getCustomer() ,
that retrieve the information from a database in the form of an object. In
addition there are the methods that operate on the transaction and a method
to store a new acquisition.
The methods startTransaction() and buy() are synchronized because they
need exclusive access to the database in order to assign a unique ID to the
new row.
package MarketDB;
import java.sql.*;
import javax.sql.*;
import java.util.Vector;
import java.util.List;
public class PersistentDB implements DBManager {
private String connection;
// creates the db manager storing the connection string
public PersistentDB(String connection){
this .connection # connection;}
// utility method to simplify the execution of queries
ResultSet select(String query) throws SQLException {
Connection conn # DriverManager
Statement stmt # conn.createStatement(
return stmt.executeQuery(query);
// creates an Item object from the table ITEM
public Item getItem( long code) {
try {
ResultSet rs # select("SELECT * FROM ITEM WHERE ID # "
! code);
if ( return new Item(rs);
} catch (SQLException e){ }
return null ;
// creates an Employee object from the table EMPLOYEE
public Employee getEmployee( long code) { ... }
// creates a Customer object from the table CUSTOMER
public Customer getCustomer( long code) { ... }
// starts a customer transaction
Search WWH ::

Custom Search