Using Static Statements
Statement st = db.createStatement();
ResultSet rs = st.executeQuery(<query>);
<query> is any standard SQL query (SELECT command)
ResultSetMetaData rsmeta = rs.getMetaData();
while(rs.next())
{
...
}
rs.close();
st.close();
db.close();
Using Prepared Statements
+ START OF MATERIAL FROM RELEASE NOTES
The following functionality is now supported in the JDBC driver:
prepareStatement()
with a string containing an SQL statement, the driver sends it to the server and stores the statement identifier for later execution. The string is stored on the server and thus is not sent again when the prepared statement is executed more than once.setNull(), setInt(), setLong(), setString(), setBoolean(), setDouble(), setFloat(), setDate(), setTime(), setTimestamp()
execute()
, executeQuery()
, or executeUpdate()
, the driver sends the statement identifier and parameter values to the server and returns a result set or an error. The driver also returns semantic and syntactic errors at this point.JDBC Example
PreparedStatement pst = dbconn.prepareStatement(
"select * from fact where type=?");
pst.setString(1, "trades");
ResultSet rs = pst.executeQuery();
JDBCSetupTest.printResults(rs, resultStream);
- END OF MATERIAL FROM RELEASE NOTES