PreparedStatement l_stmt = null;
PreparedStatement l_stmtFetch = null;
PreparedStatement l_stmtClose = null;
ResultSet l_rset = null;
try { //exception
try { //finally
//open the cursor
l_stmt = m_dbcon.prepareStatement("DECLARE FOO CURSOR FOR SELECT BAR FROM FOOBAR");
l_stmt.execute();
l_stmtFetch = m_dbcon.prepareStatement("FETCH FORWARD 10 FROM FOO");
while (true) {
//perform a fetch from the cursor (possibly multiple fetches will be done)
l_rset = l_stmtFetch.executeQuery();
l_rows = 0;
while (l_rset.next()) {
String l_bar = l_rset.getString(1);
//do something useful with the data
l_rows++;
}
l_rset.close();
l_rset = null;
if (l_rows == 0) {
//no more rows, so we are done
break;
}
}
//don't forget to close the cursor
l_stmtClose = m_dbcon.prepareStatement("CLOSE FOO");
l_stmtClose.execute();
} finally {
if (l_rset != null) {
l_rset.close();
}
if (l_stmt != null) {
l_stmt.close();
}
if (l_stmtFetch != null) {
l_stmtFetch.close();
}
if (l_stmtClose != null) {
l_stmtClose.close();
}
}
} catch (SQLException l_se) {
//do something useful here
}文章標籤
全站熱搜
