使用 SELECT 语句可从数据库中检索信息。执行 SELECT 语句时,PreparedStatement.executeQuery 方法返回一个 ResultSet 对象。
将新 SQL 语句准备为一个字符串。
String sql_string = "SELECT * FROM Department ORDER BY dept_no"; |
将该字符串传递给 PreparedStatement。
PreparedStatement select_statement = conn.prepareStatement(sql_string); |
执行该语句并将查询结果指派给 ResultSet。
ResultSet cursor = select_statement.executeQuery(); |
遍历该 ResultSet 并检索数据。
// Get the next row stored in the ResultSet. cursor.next(); // Store the data from the first column in the table. int dept_no = cursor.getInt(1); // Store the data from the second column in the table. String dept_name = cursor.getString(2); |
关闭 ResultSet 以释放资源。
cursor.close(); |
关闭 PreparedStatement 以释放资源。
select_statement.close(); |
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |