Java Reference
In-Depth Information
95
// add to the Users table
96
public boolean
addUser
(
User user
)
throws
SQLException
,
IOException
,
97
ClassNotFoundException
98
{
99
boolean
result =
false
;
100
101
String
dbUserID;
102
String
dbLastName;
103
String
dbFirstName;
104
Password dbPswd;
105
boolean
isAdmin;
106
107
dbUserID = user.getUserID
()
;
108
109
if
(
getUser
(
dbUserID
)
==
null
)
110
{
111
dbLastName = user.getLastName
()
;
112
dbFirstName = user.getFirstName
()
;
113
Password pswd = user.getPassword
()
;
114
isAdmin = user.isAdmin
()
;
115
116
PreparedStatement
pStmt = con.prepareStatement
(
117
"INSERT INTO Users VALUES (?,?,?,?,?)"
)
;
118
119
pStmt.setString
(
1, dbUserID
)
;
120
pStmt.setString
(
2, dbLastName
)
;
121
pStmt.setString
(
3, dbFirstName
)
;
122
pStmt.setBytes
(
4, serializeObj
(
pswd
))
;
123
pStmt.setBoolean
(
5, isAdmin
)
;
124
pStmt.executeUpdate
()
;
125
pStmt.close
()
;
126
result =
true
;
127
}
128
else
129
throw new
IOException
(
"User exists - cannot add."
)
;
130
131
return
result;
132
}
133
134
// add to the UserStocks table
135
public void
addUserStocks
(
String
userID,
String
stockSymbol
)
136
throws
SQLException
,
IOException
,
ClassNotFoundException
137
{
138
Statement
stmt = con.createStatement
()
;
139
140
stmt.executeUpdate
(
"INSERT INTO UserStocks VALUES ('"
141
+userID+
"'"
142
+
",'"
+stockSymbol+
"')"
)
;
143
stmt.close
()
;
144
}
145
146
////////////////////////////////////////////////////////////////////////////
147
// Methods for updating a record in a table
148
////////////////////////////////////////////////////////////////////////////
149
150
// updating the Users table
151
public boolean
updUser
(
User user
)
throws
SQLException
,
IOException
,
152
ClassNotFoundException
153
{
154
boolean
result =
false
;
155
156
String
dbUserID;
157
String
dbLastName;
158
String
dbFirstName;
159
Password dbPswd;
160
boolean
isAdmin;
161
162
dbUserID = user.getUserID
()
;
163
164
if
(
getUser
(
dbUserID
)
!=
null
)
165
{
166
dbLastName = user.getLastName
()
;
167
dbFirstName = user.getFirstName
()
;
168
Password pswd = user.getPassword
()
;
169
isAdmin = user.isAdmin
()
;
170
(continued)
FIGURE 11-36
Search WWH ::
Custom Search