无法执行DAO准备好的语句

时间:2019-05-03 15:51:59

标签: java sqlite

因此,目前我正尝试通过遵循此答案来获取自动递增的主键 Primary key from inserted row jdbc?,但显然程序甚至无法到达该行,并且在调试程序后错误出现在ps.executeQuery()行上,它显示的错误仅是"executeQuery" is not a known variable in the current context.那行,而没有对我来说没有道理那我该如何克服这个障碍?

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;

    }

方法的一部分,该方法调用createNewLoginUsers方法

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) {

                }

编辑: 已添加数据库表,它在下面的提供的链接中 https://imgur.com/a/Dggp2kc并用一种方法编辑代码,而不是2个try块,我将其放入了另一种相似的方法中,将代码更新为当前的代码。

0 个答案:

没有答案