Java Reference
In-Depth Information
desc = rs.getString("Description");
qty = rs.getInt("Qty");
cost = rs.getFloat("Cost");
System.out.println(name+", "+desc+"\t: "+qty+"\t@ $"+cost);
}
con.close();
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
The example illustrates the following main steps required to access a database and
retrieve data from a ResultSet using the JDBC API:
 
Load a JDBC driver.
 
Get a connection to the database.
 
Create a statement.
 
Execute a SQL query.
 
Retrieve data from the ResultSet.
The ResultSet provides the methods necessary to loop through the results and get
the individual database fields using methods appropriate to their respective types.
Here's an example:
Steiner 10 x 50
Binoculars
10
$799.95
Steiner 8 x 30
Binoculars
30
$299.95
PYGMY-2
Night Vision Monocular
20
$199.95
The JDBC API defines standard mappings between SQL data types and Java/JDBC
data types, including support for SQL99 advanced data types such as BLOBs and
CLOBs, ARRAYs, REFs, and STRUCTs.
This example uses the JDBC.ODBC bridge. JDBC supports a wide range
of different drivers of four distinctly different types. These are discussed in
the section on driver types later in this chapter.
Note
Search WWH ::




Custom Search