因结果集而抛出PSQLException

时间:2022-07-02 19:35:56

Im using the following code to register users.

我使用以下代码注册用户。

@POST
@Path("/userreg")
@Consumes(MediaType.APPLICATION_JSON)
public Response userRegistration(String msg) {
    String songString = "requesting";
    JSONObject jsonObj = new JSONObject(msg);
    ArrayList<Trending> output = new ArrayList<>();
    PreparedStatement stmt = null;
    try {
        Connection connection = MyResource.getConnection();
        String query = "SELECT * FROM users where username = ? OR mail  = ?;";
        PreparedStatement checkStmt = connection.prepareStatement(query);
        checkStmt.setString(1, jsonObj.getString("username"));
        checkStmt.setString(2, jsonObj.getString("mail"));
        ResultSet rs = checkStmt.executeQuery();
        if (rs.getString("username").equals(jsonObj.getString("username")) | rs.getString("mail").equals(jsonObj.getString("mail"))){

        } else {
             stmt = connection.prepareStatement(
                    "INSERT INTO USERS ( username , mail , dob , age , gender , mac , ip , password ,  registration ) VALUES( ?, ? , ? , ? , ? , ? , ? , ? , ? ) ;");
            stmt.setString(1, jsonObj.getString("username"));
            stmt.setString(2, jsonObj.getString("mail"));
            stmt.setString(3, jsonObj.getString("dob"));
            stmt.setString(4, jsonObj.getString("age"));
            stmt.setString(5, jsonObj.getString("gender"));
            stmt.setString(6, jsonObj.getString("mac"));
            stmt.setString(7, jsonObj.getString("ip"));
            stmt.setString(8, Utilities.randomAlphaNumeric(7));
            stmt.setString(9, jsonObj.getString("registration"));
            stmt.executeQuery();
        }

        songString = "success";

    } catch (Exception ex) {
        songString = "failure" + ex;
    }
    return Response.status(200).entity(songString).build();
}

Im getting the following error

我得到以下错误

failureorg.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.

failureorg.postgresql.util.PSQLException:ResultSet未正确定位,也许您需要调用next。

How can I be able to sort this out? I want to check if the username or mail address exists already. and only if it exists, i wish to let the user to register.

我怎样才能解决这个问题?我想检查用户名或邮件地址是否已存在。并且只有它存在,我希望让用户注册。

1 个解决方案

#1


1  

You need to call next method on resultset object. A resultSet cursor is initially positioned before the first row; the first call to the method next() makes the first row the current row.

您需要在resultset对象上调用next方法。 resultSet游标最初位于第一行之前;第一次调用方法next()使第一行成为当前行。

ResultSet rs = checkStmt.executeQuery();
if (rs.next() && (rs.getString("username").equals(jsonObj.getString("username")) | rs.getString("mail").equals(jsonObj.getString("mail")))){

#1


1  

You need to call next method on resultset object. A resultSet cursor is initially positioned before the first row; the first call to the method next() makes the first row the current row.

您需要在resultset对象上调用next方法。 resultSet游标最初位于第一行之前;第一次调用方法next()使第一行成为当前行。

ResultSet rs = checkStmt.executeQuery();
if (rs.next() && (rs.getString("username").equals(jsonObj.getString("username")) | rs.getString("mail").equals(jsonObj.getString("mail")))){