使用javafx无法正常工作插入数据库

时间:2017-07-06 07:52:28

标签: java spring javafx

我是新的javafx所以请原谅我的愚蠢错误我用javafx创建了一个注册fxml,但是当我尝试存储数据时,它总是把它扔进我的模型控制器的catch块,我有一个sql的插入查询

这是My RegistrationController

        public void Registersign(ActionEvent event){
        try {
        String Fname=fname.getText();
        String Lname=lname.getText();
        String Email=email.getText();
        String TXTusr=txtUser.getText();
        String TXTpass=txtPass.getText();
        String TXTPassconf=txtPassconf.getText();if(Fname.isEmpty()||Lname.isEmpty()||Email.isEmpty()||TXTusr.isEmpty()||TXTpass.isEmpty()){

                Alert alert=new Alert(Alert.AlertType.ERROR);
                alert.setHeaderText(null);
                alert.setContentText("Please Fill All DATA");
                alert.showAndWait();
                return;

        }
            loginModel.isRegister(TXTusr,TXTpass, Fname,Lname, Email);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这是我的模型控制器,用于将我的注册数据插入数据库

          public void isRegister(String user,String pass,String firstname,String lastname,String email) throws SQLException
{
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null;
      String sql = "INSERT INTO login (username, password, firstname,lastname, email) VALUES (?, ?, ?,?, ?)";
      try {
    preparedStatement=Con.prepareStatement(sql);
    preparedStatement.setString(1, user);
    preparedStatement.setString(2, pass);
    preparedStatement.setString(3, firstname);
    preparedStatement.setString(4, lastname);
    preparedStatement.setString(5, email);
     resultSet=preparedStatement.executeQuery();


        System.out.println("A new user was inserted successfully!");


} catch (Exception e) {
    // TODO: handle exception
       System.out.println("A new user was insertion failed!");
}   

它总是抛出modelContoller中的catch块我通过调试器检查了它

1 个答案:

答案 0 :(得分:3)

如果使用非executeUpdate() SQL语句,请使用executeQuery()而不是SELECT

请参阅:http://docs.oracle.com/javase/8/docs/api/java/sql/PreparedStatement.html#executeUpdate--