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

SQL Anywhere 12.0.1 » UltraLite - Java 编程 » UltraLiteJ 应用程序开发 » 使用 SQL 语句创建和修改数据

 

使用 SELECT 检索数据

使用 SELECT 语句可从数据库中检索信息。执行 SELECT 语句时,PreparedStatement.executeQuery 方法返回一个 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();
 另请参见