Java Reference
In-Depth Information
try
{
System.out.println();
//Step 4…
while (results.next())
{
System.out.println("Account no. "
+ results.getInt(1));
System.out.println("Account holder: "
+ results.getString(3)
+ " "
+ results.getString(2));
System.out.printf("Balance: %.2f %n%n"
+ results.getFloat(4));
}
}
catch(SQLException sqlEx)
{
System.out.println(
"* Error retrieving data! *");
sqlEx.printStackTrace();
System.exit(1);
}
//(No further queries, so no Step 5!)
try
{
//Step 6…
connection.close();
}
catch(SQLException sqlEx)
{
System.out.println(
"* Unable to disconnect! *");
sqlEx.printStackTrace();
System.exit(1);
}
}
}
Alternatively, we could put everything into a single try block that is followed
by code that handles all SQLException s. However, we would not then be able to
give specifi c SQL error messages.
The output from this program when executed for an Accounts table that holds
just three rows of data will be similar to that shown below (Fig. 7.1 ).
Search WWH ::




Custom Search