|
| 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 | + |
0 commit comments