0

我将以下查询传递给 ResultSet 对象:

String query = "SELECT COUNT( DISTINCT KEY ), SOURCE FROM MY_TBL\n" +
            "GROUP BY SOURCE\n" +
            "ORDER BY SOURCE";

我想捕获我为每个 SOURCE 获得的计数并将它们加总。我如何通过 ResultSet 捕获这些计数,因为 COUNT 不是 ResultSet 中的列名,我认为我不能通过返回它的值rs.getInt("COUNT")

4

4 回答 4

9

getInt重载,使用索引(an )而不是int列名:

 rs.getInt(1); // the first column is 1
于 2013-04-25T18:49:43.270 回答
0

尝试使用别名

String query = "SELECT COUNT( DISTINCT KEY ) AS COUNT, SOURCE FROM MY_TBL\n" +
        "GROUP BY SOURCE\n" +
        "ORDER BY SOURCE";
于 2013-04-25T18:54:39.463 回答
0

我认为最好使用

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * from Customer");
ResultSetMetaData rsmd = rs.getMetaData();

int numCols = rsmd.getColumnCount();
于 2014-08-17T17:30:39.247 回答
-1

我认为“getColumnCount”重新定义表中的列数而不是行数......

于 2014-12-05T19:29:19.973 回答