Connection con = DriverManager.getConnection(url, dbUser, dbPassword);
Once the Connection is established, the program can read from and write to the database by creating a
Statement object and supplying the Statement with the SQL code to be executed. For instance:
Statement stmt = con.createStatement();
// RETRIEVE STUDENT DATA
String query = "SELECT Sname, Dorm, Room, Phone FROM Student";
The Statement object has two methods, one for reading from the database, and one for updating the
database.
// 1) executeUpdate??”statements that modify the database
// 2) executeQuery??”SELECT statements (reads)
// For a SELECT SQL statement, use the executeQuery method??”
// The query returns a ResultSet object
ResultSet rs = stmt.executeQuery(query);
Instead of returning a single value, queries return a set of rows. In JDBC, the set of rows is called
a ResultSet. To inspect the values in the rows of the ResultSet, use the next() method of the
ResultSet to move among the rows, and use the appropriate ???getter??? method to retrieve the values.
// The ResultSet object is returned
// with a cursor pointing above row 1.
// The next() method of the ResultSet
// moves the cursor to the next row.
// The next() method will return FALSE
// when the cursor moves beyond the last row.
Pages:
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447