Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » UltraLiteJ » 使用 UltraLiteJ » 开发 UltraLiteJ 应用程序 » 使用 SQL 访问和操作数据

 

使用 SELECT 检索数据

可以使用 PreparedStatement 的 executeQuery 方法来检索数据,该方法使用用户定义的 SQL 语句查询数据库,并将查询结果作为 ResultSet 返回。然后,可对该 ResultSet 进行遍历以读取查询到的数据。

浏览 ResultSet 对象

ResultSet 包含以下方法来浏览 SQL SELECT 语句的查询结果:

  • next   移至下一行。

  • previous   移至上一行。

使用 ResultSet 检索数据
♦  从数据库选择数据
  1. 将新 SQL 语句准备为一个字符串。

    String sql_string = 
        "SELECT * FROM Department ORDER BY dept_no";
  2. 将该字符串传递给 PreparedStatement。

    PreparedStatement select_statement = 
        conn.prepareStatement(sql_string);
    
  3. 执行该语句并将查询结果指派给 ResultSet。

    ResultSet cursor = 
        select_statement.executeQuery();
  4. 遍历该 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);
  5. 关闭 ResultSet 以释放资源。

    cursor.close();
  6. 关闭 PreparedStatement 以释放资源。

    select_statement.close()