Java Reference
In-Depth Information
309
// get a stock description from the Stocks table
310
public String getStockDesc ( String stockSymbol )
311
throws SQLException , IOException , ClassNotFoundException
312
{
313
Statement stmt = con.createStatement () ;
314
String stockDesc = null ;
315
316
ResultSet rs = stmt.executeQuery ( "SELECT symbol, name FROM Stocks "
317
+ "WHERE symbol = '" +stockSymbol+ "'" ) ;
318
if ( rs.next ())
319
stockDesc = rs.getString ( "name" ) ;
320
rs.close () ;
321
stmt.close () ;
322
323
return stockDesc;
324
}
325
326
327
// Methods to access a complete record
328
329
// get User data from the Users table
330
public User getUser ( String userID ) throws SQLException , IOException ,
331
ClassNotFoundException
332
{
333
Statement stmt = con.createStatement () ;
334
335
String dbUserID;
336
String dbLastName;
337
String dbFirstName;
338
Password dbPswd;
339
boolean isAdmin;
340
341
byte [] buf = null ;
342
User user = null ;
343
ResultSet rs = stmt.executeQuery ( "SELECT * FROM Users WHERE userID = '"
344
+userID+ "'" ) ;
345
if ( rs.next ())
346
{
347
dbUserID = rs.getString ( "userID" ) ;
348
dbLastName = rs.getString ( "lastName" ) ;
349
dbFirstName = rs.getString ( "firstName" ) ;
350
351
// Do NOT use with JDK 1.2.2 using JDBC-ODBC bridge as
352
// SQL NULL data value is not handled correctly.
353
buf = rs.getBytes ( "pswd" ) ;
354
dbPswd= ( Password ) deserializeObj ( buf ) ;
355
356
isAdmin = rs.getBoolean ( "admin" ) ;
357
user = new User ( dbUserID,dbFirstName,dbLastName,dbPswd,isAdmin ) ;
358
}
359
rs.close () ;
360
stmt.close () ;
361
362
return user; // User object created for userID
363
}
364
365
366
// Methods to access a list of records
367
368
// get list of selected fields for all records from the Users Table
369
public ArrayList listUsers () throws SQLException , IOException ,
370
ClassNotFoundException
371
{
372
ArrayList aList = new ArrayList () ;
373
Statement stmt = con.createStatement () ;
374
375
ResultSet rs = stmt.executeQuery ( "SELECT userID, firstName, lastName, admin "
376
+ "FROM Users ORDER BY userID" ) ;
377
FIGURE 11-36
Search WWH ::




Custom Search