Java Reference
In-Depth Information
236
try
237
{
// holdings deleted, now delete user
238
stmt.executeUpdate
(
"DELETE FROM Users WHERE "
239
+
"userID = '"
+dbUserID+
"'"
)
;
240
}
241
catch
(
SQLException
ex
)
242
{
243
throw new
SQLException
(
"User deletion failed: "
+ex.getMessage
())
;
244
}
245
}
246
else
247
throw new
IOException
(
"User not found in database - cannot delete."
)
;
248
249
try
250
{
251
con.commit
()
;
252
}
253
catch
(
SQLException
ex
)
254
{
255
throw new
SQLException
(
"Transaction commit failed: "
+ex.getMessage
())
;
256
}
257
}
258
catch
(
SQLException
ex
)
259
{
260
try
261
{
262
con.rollback
()
;
263
}
264
catch
(
SQLException
sqx
)
265
{
266
throw new
SQLException
(
"Transaction failed then rollback failed: "
267
+sqx.getMessage
())
;
268
}
269
270
// Transaction failed, was rolled back
271
throw new
SQLException
(
"Transaction failed; was rolled back: "
272
+ex.getMessage
())
;
273
}
274
275
stmt.close
()
;
276
}
277
278
// delete a record from the UserStocks table
279
public void
delUserStocks
(
String
userID,
String
stockSymbol
)
280
throws
SQLException
,
IOException
,
ClassNotFoundException
281
{
282
Statement
stmt = con.createStatement
()
;
283
ResultSet
rs;
284
285
stmt.executeUpdate
(
"DELETE FROM UserStocks WHERE "
286
+
"userID = '"
+userID+
"'"
287
+
"AND symbol = '"
+stockSymbol+
"'"
)
;
288
289
rs = stmt.executeQuery
(
"SELECT symbol FROM UserStocks "
290
+
"WHERE symbol = '"
+stockSymbol+
"'"
)
;
291
292
if
(
!rs.next
())
// no users have this stock
293
delStock
(
stockSymbol
)
;
294
295
stmt.close
()
;
296
297
}
298
299
////////////////////////////////////////////////////////////////////////////
300
// Methods for listing record data from a table
301
// Ordered by:
302
//
methods that obtain individual field(s),
303
//
methods that obtain a complete record, and
304
//
methods that obtain multiple records
305
////////////////////////////////////////////////////////////////////////////
306
307
// Methods to access one or more individual fields
308
FIGURE 11-36
(continued)
Search WWH ::
Custom Search