结果集

要创建一个 ResultSet,你应该创建一个 StatementPrepapredStatement

使用 Statement 创建 ResultSet

try {
    Class.forName(driver);
    Connection connection = DriverManager.getConnection(
            "jdbc:somedb://localhost/databasename", "username", "password");
    Statement statement = connection.createStatement();
    ResultSet result = statement.executeQuery("SELECT * FROM my_table");

} catch (ClassNotFoundException | SQLException e) {
}

使用 PrepapredStatement 创建 ResultSet

try {
    Class.forName(driver);
    Connection connection = DriverManager.getConnection(
            "jdbc:somedb://localhost/databasename", "username", "password");
    PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM my_table");
    ResultSet result = preparedStatement.executeQuery();

} catch (ClassNotFoundException | SQLException e) {
}

检查 ResultSet 是否包含信息

if (result.next()) {
   //yes result not empty                
}

从 ResultSet 获取信息

你可以从 ResultSet 获得几种类型的信息,例如 String, int, boolean, float, Blob,…获取你必须使用循环或简单信息的信息:

if (result.next()) {
    //get int from your result set
    result.getInt("id");
    //get string from your result set
    result.getString("username");
    //get boolean from your result set
    result.getBoolean("validation");
    //get double from your result set
    result.getDouble("price");
}