Java Reference
In-Depth Information
3. The program uses the getConnection() method in the sample program to
get the connection. It then creates a statement.
Connection connection = getConnection();
Statement stmt = connection.createStatement();
4. You query the Contacts table, retrieving all rows and all columns.
// Get everything from Contacts table in mydatabase.mdb.
ResultSet rs = stmt.executeQuery("select * from contacts");
5. You iterate through the result set, display the results, and then free all the
resources.
// Iterate result set and print rows.
while (rs.next())
{
System.out.println("name=" + rs.getString(1));
System.out.println("phone num=" + rs.getString(2));
System.out.println("email=" + rs.getString(3));
System.out.println("note=" + rs.getString(4));
}
// Close down.
rs.close();
stmt.close();
closeConnection();
6. The output for the sample table looks like this:
name=Susan Cobb
phone num=777-555-1212
company=Sun
email=scobb@sun.com
note=Not a real person
name=Bill Smith
phone num=121-333-3433
company=Empire Bank
email=bsmith@empire.com
note=Not a real person
name=John Smith
phone num=444-343-3333
company=XYZ Corp
email=jsmith@xyz.com
note=Not a real person
Search WWH ::




Custom Search