从db resultset java检索单列的多个行

时间:2017-03-13 10:38:51

标签: java database jdbc resultset

任何人都可以帮助我使用java

从db resultset为单个列检索多行

例如:

id amount
1  245.0
2  245.0

1 个答案:

答案 0 :(得分:1)

这是一个简单的例子:

try {
    Class.forName("driver");
    Connection connection = DriverManager.getConnection("url", "username", "password");
    Statement statement = connection.createStatement();
    ResultSet result = statement.executeQuery("SELECT id FROM my_table");
    List<Integer> list = new ArrayList<>();

    while (result.next()) {
      list.add(result.getInt("id"));
    }

    //you can loop throw your list and do your action here

} catch (ClassNotFoundException | SQLException e) {
}

您可以在此处详细了解:PreparedStatementResultSet