· 9 years ago · Dec 02, 2016, 08:46 PM
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
3
4<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/AJAX_Template.dwt" codeOutsideHTMLIsLocked="false" -->
5<head>
6<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
8
9<!-- InstanceBeginEditable name="doctitle" -->
10<title>Eric Stratmeyer</title>
11<!-- InstanceEndEditable -->
12<!-- InstanceBeginEditable name="head" -->
13<style type="text/css">
14.container .content .displayAll {
15 text-align: center;
16}
17.container .content {
18 text-align: center;
19}
20</style>
21<!-- InstanceEndEditable -->
22<style type="text/css">
23<!--
24body {
25 font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
26 background-color: #4E5869;
27 margin: 0;
28 padding: 0;
29 color: #000;
30}
31
32/* ~~ Element/tag selectors ~~ */
33ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
34 padding: 0;
35 margin: 0;
36}
37h1, h2, h3, h4, h5, h6, p {
38 margin-top: 0; /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
39 padding-right: 15px;
40 padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
41}
42a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
43 border: none;
44}
45
46/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
47a:link {
48 color:#414958;
49 text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
50}
51a:visited {
52 color: #4E5869;
53 text-decoration: underline;
54}
55a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
56 text-decoration: none;
57}
58
59/* ~~ this container surrounds all other divs giving them their percentage-based width ~~ */
60.container {
61 width: 60%;
62 /*max-width: 1260px;*//* a max-width may be desirable to keep this layout from getting too wide on a large monitor. This keeps line length more readable. IE6 does not respect this declaration. */
63 /*min-width: 780px;*//* a min-width may be desirable to keep this layout from getting too narrow. This keeps line length more readable in the side columns. IE6 does not respect this declaration. */
64 background-color: #FFF;
65 margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout. It is not needed if you set the .container's width to 100%. */
66}
67
68/* ~~the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo~~ */
69.header {
70 background-color: #6F7D94;
71 text-align: center;
72}
73
74/* ~~ This is the layout information. ~~
75
761) Padding is only placed on the top and/or bottom of the div. The elements within this div have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design.
77
78*/
79.content {
80 padding: 10px 0;
81}
82
83/* ~~ This grouped selector gives the lists in the .content area space ~~ */
84.content ul, .content ol {
85 padding: 0 15px 15px 40px; /* this padding mirrors the right padding in the headings and paragraph rule above. Padding was placed on the bottom for space between other elements on the lists and on the left to create the indention. These may be adjusted as you wish. */
86}
87
88/* ~~ The footer ~~ */
89.footer {
90 padding: 10px 0;
91 background-color: #6F7D94;
92}
93
94/* ~~ miscellaneous float/clear classes ~~ */
95.fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
96 float: right;
97 margin-left: 8px;
98}
99.fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
100 float: left;
101 margin-right: 8px;
102}
103.clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
104 clear:both;
105 height:0;
106 font-size: 1px;
107 line-height: 0px;
108}
109-->
110</style></head>
111
112<body>
113
114<div class="container">
115 <div class="header">
116 <h2>Murach DB Application</h2>
117 <h2> 3 Tier Website</h2>
118 <!-- end .header --></div>
119 <div class="content"><!-- InstanceBeginEditable name="Content" -->
120
121 <form action="DisplayOne.jsp"><input type="submit" name="displayOneRecord" value="Display One Record" /></form>
122<form action="DisplayAll.jsp"><input type="submit" name="displayAll" value="Display All Records" /></form>
123<form action="AddRecord.jsp"><input type="submit" name="addRecord" value="Add a Record" /></form>
124<form action="DeleteRecord.jsp"><input type="submit" name="deleteRecord" value="Delete a Record" /></form>
125<form action="UpdateRecord.jsp"><input type="submit" name="updateRecord" value="Update a Record" />
126</form>
127
128 <!-- InstanceEndEditable --><!-- end .content --></div>
129 <div class="footer">
130 <p>©2014 by Southeast Tech Staff</p>
131 <!-- end .footer --></div>
132 <!-- end .container --></div>
133
134 <form action = "DisplayOne.jsp" >
135Primary Key: <input type="text" name="primaryKey" size="20"><br>
136<br>
137<input type="submit" name="submitPrimaryKey" value="SUBMIT">
138<br></form>
139<br>
140<p><a href="index.html">Back to Home</a><br></p>
141
142<form action = "AddRecord.jsp">
143 Code: <input type="text" name="code" size="20"> <br>Description:
144 <input type="text" name="description" size="40"> <br>Price:
145 <input type="text" name="price" size="20"><br> <br>
146 <input type="submit" name="submit" value="ADD Record">
147 </form>
148
149 <p>
150 <a href="index.html">Back to Home</a><br>
151 </p> <p></p>
152
153 <form action="DeleteRecord.jsp">
154 Primary Key: <input type="text" name="primaryKey" size="20"><br>
155 <br> <input type="submit" name="submitPrimaryKey"
156 value="DELETE Record"> <br>
157 </form>
158 <br>
159 <p>
160 <a href="index.html">Back to Home</a><br>
161 </p>
162
163 <form action="UpdateRecord.jsp">
164 Primary Key: <input type="text" name="primaryKey" size="20"><br>
165 <br> <input type="submit" name="submit"
166 value="FIND RECORD TO UPDATE"> <br>
167 </form>
168 <br>
169 <p>
170 <a href="index.html">Back to Home</a><br>
171 </p>
172
173 <%
174
175String input = request.getParameter("primaryKey");
176String output = "";
177
178//out.print("input now = " + input + "<br /> <br />");
179if ( (input != null)&& !input.equals("") ) //user validation
180{
181 ProductDB productDB = new ProductDB(); //create a ProductDB object so that we can dynamically call methods in it
182
183 //test getProduct( ) method
184 Product product = productDB.getProduct(input); //call getProduct() method in productDB object & pass it the primary key
185
186 if(product == null) //test to see if record is found (or SQL error)- i.e. a null was returned
187 {
188 output = "Product " + input + " NOT Found";
189 }
190 else
191 { //here if a valid product object was returned by getProduct( )
192
193 output = "<h3>OUTPUT FROM getProduct(" + input + ")</h3>"
194 + product.toString()
195 + "<br><br><br>";
196 }
197 }
198 else{
199 output = "Please enter a Product Code";
200 }
201
202 out.print(output);
203
204
205
206 String output = ""; //create String object to place output information in
207
208 ProductDB productDB = new ProductDB(); //create a new ProductDB object by calling its default constructor method
209
210 //get an ArrayList with all the product objects - records in it by calling the getProducts() method in the productDB object
211 ArrayList<Product> products = productDB.getProducts();
212
213 if (products == null) {
214 output += "Error! Unable to get products."; //here if getProducts() method returned a null
215 } else { //here if prouducts object not a null - i.e., an ArrayList<> was returned rather than a null
216 output = "<h3>Number of records in ArrayList object = " + products.size() + "</h3>"; //display number of objects in ArrayList<> object
217 Product p = null; //declare a null Product object to be used in for loop for temporarily storing Product objects in ArrayList<> object
218 for (int i = 0; i < products.size(); i++) {
219 p = (Product) products.get(i); //get index i Product object from ArrayList<> (note "i" will be between 0 and products.size())
220 output += p.toString() + "<br />"; //concatenate current output String with properties (database record) from p object
221 }
222 }
223
224 out.print(output); //display either Error report from above or concatenated String with all the records from the ArrayList<>
225
226
227
228 String aCode = request.getParameter("code");
229 String aDescription = request.getParameter("description");
230 String aPrice = request.getParameter("price");
231
232 String output = "";
233 //test that it's not the first time (html form not submitted) && that data is entered in all fields
234 if (aCode != null && !aCode.equals("") && !aDescription.equals("")
235 && !aPrice.equals("")) {
236 try {
237 double dblPrice = Double.parseDouble(aPrice);
238
239 output = "<h3>OUTPUT FROM addProduct( ) method</h3>";
240 Product newProduct = new Product(aCode, aDescription,
241 dblPrice);
242
243 ProductDB productDB = new ProductDB(); //ProductDB methods are no longer static methods,
244 // so need to create a ProductDB object
245
246 //note - addProduct returns a false if newProduct already in DB
247 boolean boolAdd = productDB.addProduct(newProduct);
248 if (boolAdd) //check to see if product was sucessfully added
249 {
250 output += aCode + " record sucessfully added<br><br>";
251 } else {
252 output += "Error: " + aCode
253 + " record already exists in DB<br><br>";
254 }
255 }catch (NumberFormatException err)
256 {
257 output += "Please enter a valid number in your price textfield: " + aPrice;
258 }
259 catch (Exception e) {
260 output += "Error: " + e.toString();
261 }
262 } else {
263 output = "Please enter a product code, description and a price: ";
264 }
265
266 out.print(output);
267
268
269 String input = request.getParameter("primaryKey");
270 String output = "";
271 ProductDB productDB = new ProductDB(); //created a ProductDB object to allow calling "dynamic/non-static" methods
272 out.print(productDB.connect()); //Display result of trying to connect to database
273 //out.print("User entered: " + input);
274 if (input != null && input.length() == 4) {
275 Product productInput = productDB.getProduct(input);
276 if (productInput != null) {
277
278 //create a Product object with the input String as its code/primary key
279 //using one of the parametized constructor methods
280 output += "<h3>OUTPUT FROM deleteProduct( ) method </h3>";
281 boolean boolDel = productDB.deleteProduct(productInput); //returns true if able to delete record or if not in database
282 if (boolDel) //check to see if product deleted successfully
283 {
284 output += input
285 + " deleted from DB or not in the database table"
286 + "<br><br><br>";
287 } else {
288 output += "Error: could not delete " + input;
289 }
290 } else {
291 output += input + " not in database";
292 }
293 } else {
294 output += "Please enter a valid Primary Key with 4 characters";
295 }
296
297 out.print(output);
298
299
300 String input = request.getParameter("primaryKey");
301
302 if (input != null && !input.equals("")) {
303 ProductDB productDB = new ProductDB();
304
305 //test getProduct( ) method
306 Product product = productDB.getProduct(input);
307 //Note - getProduct( ) method returns a null if either the record not found OR if SQL Error
308 if (product != null) { //Here if a product object returned by the getProduct( ) method
309 %>
310
311 <form action="UpdateRecordSubmit.jsp">
312 <b>Code: <%=product.getCode()%></b><br /> <input type="hidden"
313 name="code" value="<%=product.getCode()%>"> Description: <input
314 type="text" name="description" size="20"
315 value="<%=product.getDescription()%>"><br>Price: <input
316 type="text" name="price" size="20" value="<%=product.getPrice()%>"><br>
317 <br> <br> Please modify above information if necessary and
318 then click on Update Record button<br>
319 <input type="submit" name="submit" value="Update Record">
320 </form>
321 <%
322 } else { //NOTE - also here if SQL error when running the getProduct() method, but we'll assume that method is good
323 out.print("Product with product code: " + input
324 + " not found.");
325 }
326
327 } else {
328 out.print("Please enter a 4 character primary key");
329 }
330
331
332 //get latest updated information
333 String aCode = request.getParameter("code");
334 String aDescription = request.getParameter("description");
335 String aPrice = request.getParameter("price");
336 String aSubmit = request.getParameter("submit");
337
338 String output = "";
339 Product newProduct = null;
340
341 if (aSubmit.equals("Update Record")) {
342
343 try {
344 double dblPrice = Double.parseDouble(aPrice);
345
346 output = "<h3>OUTPUT FROM updateProduct( ) method</h3>";
347 newProduct = new Product(aCode, aDescription, dblPrice);
348 ProductDB newProductDB = new ProductDB();
349
350 //note - addProduct returns a false if newProduct already in DB
351 boolean boolAdd = newProductDB.updateProduct(newProduct);
352 if (boolAdd) //check to see if product was sucessfully updated
353 {
354 output += aCode
355 + " record successfully updated<br><br>";
356 } else {
357 output += "Error: " + aCode
358 + " unable to update record in DB<br><br>";
359 }
360 } catch (Exception e) {
361 output += "Error: " + e.toString();
362 }
363 }
364 out.print(output);
365
366 %>
367
368</body>
369<!-- InstanceEnd --></html>