1

So currently Im trying to do get the auto-incremented primary key by following this answer Primary key from inserted row jdbc? but apparently the program can't even reach that line and the error appeared on the ps.executeQuery() line after debugging the program, the error it display was only "executeQuery" is not a known variable in the current context. that line, which didn't make sense to me. So how do I go pass this hurdle?

public static int createNewLoginUsers(String newPassword, String newRole) throws SQLException{
        Connection conn = DBConnection.getConnection();
        Statement st = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        int id = 0;

        try{
            String sql = "INSERT into Login(password, role) VALUES(?,?)";           
            ps = conn.prepareStatement(sql);
            ps.setString(1, newPassword);
            ps.setString(2, newRole);
            ps.executeUpdate();

            st = conn.createStatement();
            rs = st.executeQuery("SELECT * from Login");

            rs.last();
            System.out.println(rs.getInt("username"));
            id = rs.getInt("username");

            rs.close();


        } finally{
            try{
            conn.close();
        }catch(SQLException e){
            System.out.println(e);
        }

        }
        return id;

    }

The part of the method which calls the createNewLoginUsers method

if(newPasstxtfld.getText().equals(retypeNewPasstxtfld.getText())){
                    //update into database
                    try {
                    int username = CreateUsersDAO.createNewLoginUsers( (String) newUserRoleBox.getSelectionModel().getSelectedItem(), newPasstxtfld.getText());
                    Alert confirmation = new Alert(Alert.AlertType.INFORMATION);
                    confirmation.setHeaderText("New User Details has been created. Your username is " + username);
                    confirmation.showAndWait();
                } catch (SQLException e) {

                }

EDIT: Databases table added and it's in the provided link below https://imgur.com/a/Dggp2kc and edit to the codes instead of 2 try blocks in one method i have placed it into a different similar method, updated my codes to the current one I have.

Anon555
  • 339
  • 1
  • 2
  • 12

0 Answers0