while (rs.next()) {
// Note that you must know the datatype of the column
// and use the appropriate method to read it:
// rs.getString( <"columnName" or indexStartingAtOne> )
// rs.getInt ( <"columnName" or indexStartingAtOne> )
// rs.getFloat ( <"columnName" or indexStartingAtOne> )
// others include getDate, getBoolean, getByte, getLong,
// getCharacterStream, getBlob, etc.
// These work with lots of lattitude. e.g., you can
// read a varchar2 column with getInt, if you know
// the characters comprise a number.
// getObject will read ANYTHING
// getString will read anything and covert it to a String
String studentName = rs.getString("Sname");
String studentDorm = rs.getString("Dorm");
int studentRoom = rs.getInt("Room");
String studentPhone = rs.getString("Phone");
System.out.println(studentName + ", " + studentDorm + ", " +
studentRoom + ", " + studentPhone);
}
CHAP. 8] DATABASE 163
Updating the contents of the database is done similarly, but one uses the executeUpdate() method of
the Statement object:
stmt.executeUpdate("INSERT INTO Student
VALUES('Maggie Simpson', 'Williams', 144,
'585-223-1234', 'Biology', 'Ivan Heally')");
When the program finishes reading from and writing to the database, the Statement and Connection
objects should be closed:
// DATABASE CLOSE/CLEANUP
stmt.
Pages:
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448