您好,我正在编写一个 Java 代码,该代码旨在获取我正在处理的用户配置文件表中的用户总数。现在我确定我的 sql 语句是正确的,但出于某种原因我不知道如何从中获取数值。非常感谢和欢迎任何帮助。我确信我尝试获取该值的方式不起作用,但我不知道如何获取该值,这是下面的代码,谢谢:
public int getUserCount() throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
double encinfo;
conn = DAOFactory.getDatabaseDAO().getConnection();
StringBuffer query = new StringBuffer();
query.append( "Select COUNT (DISTINCT record_id)");
query.append( "From USER_PROFILE");
stmt = conn.createStatement();
rs = stmt.executeQuery( query.toString());
if (!rs.next())
throw new SQLException( SQLException.User_NOT_FOUND,"No User Count found.");
// Should be able to get the user amount or value or total users in DB.
userInfo = rs.getDouble( "USER_VALUE");
m_userCount = (int) userInfo;
}
最佳答案
这可能有效:
为记录计数添加别名:
query.append( "Select COUNT (DISTINCT record_id) AS USER_VALUE");
query.append( " From USER_PROFILE");
另一种方式是:
rs.getDouble(1);
这里1是列的索引。
关于java-获取数字数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13352154/