【问题标题】:retrieve multiple rows for single column from db resultset java [closed]从db resultset java中检索单列的多行[关闭]
【发布时间】:2017-03-13 10:38:50
【问题描述】:

谁能帮助我使用 java 从 db 结果集中检索单列的多行

例如:

id amount
1  245.0
2  245.0

【问题讨论】:

  • 您使用什么 DBMS?
  • 你的代码在哪里?你有什么问题?
  • 你在使用 JDBC 吗?
  • 你的问题不够清楚,你的意思是你想把你的列合并成一个。
  • 好的,你在用什么? JDBC?

标签: java database jdbc resultset


【解决方案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

【讨论】:

  • 感谢您的回复,但上面的代码无法正常打印 id 1,它没有检索 id 列的第 2 行
  • 什么代码,我的代码,还是你的代码@MagicTest?
  • @YCF_L 你的代码
  • 尝试使用list.add(result.getInt("id")); 而不是@MagicTest,我已经编辑了我的答案
  • 试过你的代码不工作
猜你喜欢
  • 2021-11-27
  • 2015-09-13
  • 2012-12-11
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
相关资源
最近更新 更多