可以使用 PreparedStatement 的 executeQuery 方法来检索数据,该方法使用用户定义的 SQL 语句查询数据库,并将查询结果作为 ResultSet 返回。然后,可对该 ResultSet 进行遍历以读取查询到的数据。
ResultSet 包含以下方法来浏览 SQL SELECT 语句的查询结果:
next 移至下一行。
previous 移至上一行。
将新 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() |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |