This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddressRegistration.java
More file actions
181 lines (150 loc) · 4.08 KB
/
AddressRegistration.java
File metadata and controls
181 lines (150 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package jsf2demo;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.sql.*;
@Named
@SessionScoped
public class AddressRegistration implements java.io.Serializable {
private String lastName;
private String firstName;
private String mi;
private String telephone;
private String email;
private String street;
private String city;
private String state;
private String zip;
private String status = "Nothing stored";
// Use a prepared statement to store a student into the database
private PreparedStatement pstmt;
public AddressRegistration() {
initializeJdbc();
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMi() {
return mi;
}
public void setMi(String mi) {
this.mi = mi;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
private boolean isRquiredFieldsFilled() {
return !(lastName == null || firstName == null
|| lastName.trim().length() == 0
|| firstName.trim().length() == 0);
}
public String processSubmit() {
if (isRquiredFieldsFilled()) {
return "ConfirmAddress";
} else {
return "";
}
}
public String getRequiredFields() {
if (isRquiredFieldsFilled()) {
return "";
} else {
return "Last Name and First Name are required";
}
}
public String getInput() {
return "<p style=\"color:red\">You entered <br />"
+ "Last Name: " + lastName + "<br />"
+ "First Name: " + firstName + "<br />"
+ "MI: " + mi + "<br />"
+ "Telephone: " + telephone + "<br />"
+ "Email: " + email + "<br />"
+ "Street: " + street + "<br />"
+ "City: " + city + "<br />"
+ "Street: " + street + "<br />"
+ "City: " + city + "<br />"
+ "State: " + state + "<br />"
+ "Zip: " + zip + "</p>";
}
/** Initialize database connection */
private void initializeJdbc() {
try {
// Explicitly load a MySQL driver
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
// Establish a connection
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/javabook", "scott", "tiger");
// Create a Statement
pstmt = conn.prepareStatement("insert into Address (lastName,"
+ " firstName, mi, telephone, email, street, city, "
+ "state, zip) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
} catch (Exception ex) {
System.out.println(ex);
}
}
/** Store an address to the database */
public String storeStudent() {
try {
pstmt.setString(1, lastName);
pstmt.setString(2, firstName);
pstmt.setString(3, mi);
pstmt.setString(4, telephone);
pstmt.setString(5, email);
pstmt.setString(6, street);
pstmt.setString(7, city);
pstmt.setString(8, state);
pstmt.setString(9, zip);
pstmt.executeUpdate();
status = firstName + " " + lastName
+ " is now registered in the database.";
} catch (Exception ex) {
status = ex.getMessage();
}
return "AddressStoredStatus";
}
public String getStatus() {
return status;
}
}