Statement statement = conn.createStatement();
ResultSet rs1 = statement.executeQuery(
  "Select count(*) from user where username=? OR email=? OR phone=?"
);
int c = 0;

while (rs1.next()) {
    c++;
}

最佳答案

您的代码有一些问题:


首先,您应该使用PreparedStatement,因为您正在使用?占位符
您不应该使用while,因为查询仅返回一个结果


相反,您的代码应如下所示:

String query= "Select count(*) as cnt from user where username=? OR email=? OR phone=?";
try (PreparedStatement pstmt = conn.prepareStatement(query);) {
    pstmt.setString(1, username);
    pstmt.setString(2, email);
    pstmt.setString(3, phone);
    ResultSet rs1 = pstmt.executeQuery();
    long c = 0;
    if(rs1.next()){
        c = rs1.getLong("cnt");
    }
}

07-24 09:16