JavaWeb从ResultSet获取select count统计结果

代码实现

@Override
public int findByUsernameCount(String username) {
    
    
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet res = null;
    int count = 0;
    conn = JDBCUtils.getConn();
    String sql = "select count(*) from tb_card where username=?";
    try {
    
    
        ps = conn.prepareStatement(sql);
        ps.setString(1, username);
        res = ps.executeQuery();
        if (res.next()) {
    
    
            count = res.getInt(1);
        }
    } catch (SQLException e) {
    
    
        e.printStackTrace();
    } finally {
    
    
        JDBCUtils.close(conn, ps, res);
    }
    return count;
}

猜你喜欢

转载自blog.csdn.net/qq_43656233/article/details/107351338