任何人都可以帮助我使用java
从db resultset为单个列检索多行例如:
id amount
1 245.0
2 245.0
答案 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) {
}
您可以在此处详细了解:PreparedStatement和ResultSet