Skip to content

Commit a710a12

Browse files
authored
Add files via upload
1 parent 899f474 commit a710a12

File tree

89 files changed

+3679
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+3679
-0
lines changed

Chapter16/Example1/Customer.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Customer class for Chapter 16 example 1 ready for three tiers
2+
// Identical to Customer class in Chapter 14
3+
4+
import java.util.Vector;
5+
public class Customer
6+
{
7+
// attribute definitions
8+
private String name;
9+
private String address;
10+
private String phoneNo;
11+
12+
// constructor with parameters
13+
public Customer(String aName, String anAddress, String aPhoneNo)
14+
{
15+
// invoke accessors to populate attributes
16+
setName(aName);
17+
setAddress(anAddress);
18+
setPhoneNo(aPhoneNo);
19+
}
20+
21+
// DA static methods *********************************
22+
public static void initialize()
23+
{CustomerDA.initialize();}
24+
public static Customer find(String phoneNo)
25+
throws NotFoundException
26+
{return CustomerDA.find(phoneNo);}
27+
public static Vector getAll()
28+
{return CustomerDA.getAll();}
29+
public static void terminate()
30+
{CustomerDA.terminate();}
31+
32+
// DA instance methods *******************************
33+
public void addNew() throws DuplicateException
34+
{CustomerDA.addNew(this);}
35+
public void delete() throws NotFoundException
36+
{CustomerDA.delete(this);}
37+
public void update() throws NotFoundException
38+
{CustomerDA.update(this);}
39+
40+
// get accessors
41+
public String getName()
42+
{ return name;}
43+
public String getAddress()
44+
{ return address;}
45+
public String getPhoneNo()
46+
{ return phoneNo;}
47+
48+
// set accessors
49+
public void setName(String newName)
50+
{ name = newName;}
51+
public void setAddress(String newAddress)
52+
{ address = newAddress;}
53+
public void setPhoneNo(String newPhoneNo)
54+
{ phoneNo = newPhoneNo;}
55+
public String tellAboutSelf()
56+
{
57+
return (getName() + ", " + getAddress() + ", "
58+
+ getPhoneNo());
59+
}
60+
}

Chapter16/Example1/CustomerDA.java

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
// CustomerDA for Chapter 16 Example 1 -
2+
// Same as Chapter 14 Example 4
3+
4+
import java.util.Vector; // for Vector of customers
5+
import java.io.*; // needed for file i/o
6+
import java.sql.*; // for SQL
7+
8+
public class CustomerDA
9+
{
10+
static Customer aCustomer;
11+
12+
// The Data Source name is "Chapter16CustomerDatabase"
13+
static String url = "jdbc:odbc:Chapter16CustomerDatabase";
14+
static Connection aConnection;
15+
static Statement aStatement;
16+
17+
// declare variables for Customer attribute values
18+
static String name;
19+
static String address;
20+
static String phoneNumber;
21+
22+
// Implement the four static methods in Customer ************
23+
// initialize, find, getAll, and terminate
24+
25+
// establish the database connection
26+
public static void initialize()
27+
{
28+
try
29+
{
30+
// load the jdbc - odbc bridge driver for Windows
31+
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
32+
// create connection instance
33+
aConnection = DriverManager.getConnection(url, "", "");
34+
// create statement object instance for this connection
35+
aStatement = aConnection.createStatement();
36+
}
37+
catch (ClassNotFoundException e)
38+
{System.out.println(e);}
39+
catch (SQLException e)
40+
{ System.out.println(e); }
41+
}
42+
43+
// find an instance in the database
44+
public static Customer find(String key) throws NotFoundException
45+
{
46+
aCustomer = null;
47+
48+
// define the SQL query statement using the phone number key
49+
String sqlQuery = "SELECT Name, Address, PhoneNo " +
50+
"FROM CustomerTable " +
51+
"WHERE PhoneNo = '" + key +"'";
52+
53+
// execute the SQL query statement
54+
try
55+
{
56+
ResultSet rs = aStatement.executeQuery(sqlQuery);
57+
58+
// next method sets cursor & returns true if there is data
59+
boolean gotIt = rs.next();
60+
if (gotIt)
61+
{
62+
// extract the data
63+
String name = rs.getString(1);
64+
String address = rs.getString(2);
65+
String phoneNumber = rs.getString(3);
66+
67+
// create Customer instance
68+
aCustomer = new Customer(name, address, phoneNumber);
69+
}
70+
else
71+
{
72+
// nothing was retrieved
73+
throw (new NotFoundException("not found "));
74+
}
75+
rs.close();
76+
}
77+
78+
catch (SQLException e)
79+
{ System.out.println(e);}
80+
81+
return aCustomer;
82+
}
83+
84+
// get all instances from the database
85+
public static Vector getAll()
86+
{
87+
Vector customers = new Vector();
88+
// define the SQL query statement for get all
89+
String sqlQuery = "SELECT Name, Address, PhoneNo " +
90+
"FROM CustomerTable ";
91+
try
92+
{
93+
// execute the SQL query statement
94+
ResultSet rs = aStatement.executeQuery(sqlQuery);
95+
boolean moreData = rs.next();
96+
if (moreData)
97+
// next method sets cursor & returns true if there is data
98+
while (moreData)
99+
{
100+
// extract the data
101+
name = rs.getString(1);
102+
address = rs.getString(2);
103+
phoneNumber = rs.getString(3);
104+
// create Customer instance
105+
aCustomer = new Customer(name, address, phoneNumber);
106+
customers.addElement(aCustomer);
107+
moreData = rs.next();
108+
}
109+
rs.close();
110+
}
111+
112+
catch (SQLException e)
113+
{ System.out.println(e);}
114+
115+
return customers;
116+
}
117+
118+
// close the database connection
119+
public static void terminate()
120+
{
121+
try
122+
{ // close everything
123+
aStatement.close();
124+
aConnection.close();
125+
}
126+
catch (SQLException e)
127+
{ System.out.println(e); }
128+
}
129+
130+
// Implement the three instance methods in Customer *************
131+
// addNew, delete, update
132+
133+
// add new instance to database
134+
public static void addNew(Customer aCustomer)
135+
throws DuplicateException
136+
{
137+
// retrieve the customer attribute values
138+
name = aCustomer.getName();
139+
address = aCustomer.getAddress();
140+
phoneNumber = aCustomer.getPhoneNo();
141+
142+
// create the SQL insert statement using attribute values
143+
String sqlInsert = "INSERT INTO CustomerTable " +
144+
"(Name, Address, PhoneNo)" +
145+
" VALUES ('" +
146+
name + "', '" +
147+
address + "', '" +
148+
phoneNumber + "')";
149+
150+
// see if this customer already exists in the database
151+
try
152+
{
153+
Customer c = find(phoneNumber);
154+
throw (new DuplicateException("Customer Exists "));
155+
}
156+
157+
// if NotFoundException, add customer to database
158+
catch(NotFoundException e)
159+
{
160+
try
161+
{
162+
// execute the SQL update statement, a 1 return good
163+
int result = aStatement.executeUpdate(sqlInsert);
164+
}
165+
166+
catch (SQLException ee)
167+
{ System.out.println(ee); }
168+
}
169+
}
170+
171+
// delete an instance from the database
172+
public static void delete(Customer aCustomer)
173+
throws NotFoundException
174+
{
175+
// retrieve the phone no (key)
176+
phoneNumber = aCustomer.getPhoneNo();
177+
178+
// create the SQL delete statement
179+
String sqlDelete = "DELETE FROM CustomerTable " +
180+
"WHERE PhoneNo = '" + phoneNumber +"'";
181+
182+
// see if this customer already exists in the database
183+
// NotFoundException is thrown by find method
184+
try
185+
{
186+
Customer c = Customer.find(phoneNumber);
187+
188+
// if found, execute the SQL update statement, a 1 return
189+
// is good delete
190+
int result = aStatement.executeUpdate(sqlDelete);
191+
}
192+
193+
catch (SQLException e)
194+
{ System.out.println(e); }
195+
}
196+
197+
// update instance in the database
198+
public static void update(Customer aCustomer) throws
199+
NotFoundException
200+
{
201+
// retrieve the customer attribute values
202+
phoneNumber = aCustomer.getPhoneNo();
203+
name = aCustomer.getName();
204+
address = aCustomer.getAddress();
205+
206+
// define the SQL query statement using the phone number key
207+
String sqlUpdate = "UPDATE CustomerTable " +
208+
" SET Name = '" + name +"', " +
209+
" Address = '" + address +"' " +
210+
" WHERE PhoneNo = '" + phoneNumber +"'";
211+
212+
// see if this customer already exists in the database
213+
// NotFoundException is thrown by find method
214+
try
215+
{
216+
Customer c = Customer.find(phoneNumber);
217+
218+
// if found, execute the SQL update statement, a 1 return
219+
// is good delete
220+
int result = aStatement.executeUpdate(sqlUpdate);
221+
}
222+
223+
catch (SQLException ee)
224+
{ System.out.println(ee); }
225+
}
226+
}
227+

Chapter16/Example1/Customers.mdb

228 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class DuplicateException extends Exception
2+
{
3+
public DuplicateException(String message)
4+
{ super(message);}
5+
}

0 commit comments

Comments
 (0)