为什么从 Java 代码将信息更新到 SQL 数据库时会出现错误?

我正在为系统的注册页面编写代码。单击按钮后,系统将检查用户是否超过 14 岁。如果是,系统应将所有输入的数据保存到 SQL 数据库中。


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

       // TODO add your handling code here:

       int age = Integer.parseInt(Age_Input.getText());

       String firstname = FirstName_Input.getText();

       String surname = Surname_Input.getText();

       String email = Email_Input.getText();

       String userid = UserID_InputSignUp.getText();

       char[] pass = Password_InputSignUp.getPassword();


       if (age<14) {

           JOptionPane.showMessageDialog(null,"Sorry, You need to be at least 14 years to use this software... ");

           new Login_Page().setVisible(true);

           this.setVisible(false);

       } else {

           try {

              Class.forName("com.mysql.cj.jdbc.Driver");

              Connection connect = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/user_info", "root", "nerdswonka");

              Statement stmt = connect.createStatement();


              String query1 = "INSERT INTO user_info(user_id, password, firstname, lastname, emailid, age) VALUES('"+userid+"', "+Arrays.toString(pass)+", '"+firstname+"', '"+surname+"', '"+email+"', "+age+");";

              stmt.executeUpdate(query1);


              JOptionPane.showMessageDialog(null, "Account Creation succesful!");

              stmt.close();

              connect.close();


          } catch (Exception e) {

              JOptionPane.showMessageDialog(null, "Error in connecting to SQL Database");

        }


          new Login_Page().setVisible(true);

          this.setVisible(false);

    }


}                                                                 

该代码不会将任何内容更新到数据库中,只是在出现异常(错误)后显示 JOptionPane。可以对代码进行哪些编辑以便将值存储到 SQL 中?


慕码人8056858
浏览 157回答 2
2回答

SMILET

失败的直接原因可能是您的INSERT语句包含以下未用单引号引起来的字符串:Arrays.toString(pass)但是,您应该完全放弃当前的方法,而使用准备好的语句:String sql = "INSERT INTO user_info (user_id, password, firstname, lastname, emailid, age) " +&nbsp; &nbsp; "VALUES (?, ?, ?, ?, ?, ?)";try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_info", "root", "nerdswonka");&nbsp; &nbsp; PreparedStatement ps = conn.prepareStatement(sql)) {&nbsp; &nbsp; &nbsp; &nbsp; ps.setString(1, userid);&nbsp; &nbsp; &nbsp; &nbsp; ps.setString(2, Arrays.toString(pass));&nbsp; &nbsp; &nbsp; &nbsp; ps.setString(3, firstname);&nbsp; &nbsp; &nbsp; &nbsp; ps.setString(4, surname);&nbsp; &nbsp; &nbsp; &nbsp; ps.setString(5, email);&nbsp; &nbsp; &nbsp; &nbsp; ps.setInt(6, age);&nbsp; &nbsp; &nbsp; &nbsp; int row = ps.executeUpdate();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(row); // rows inserted (should be 1)&nbsp; &nbsp; }&nbsp; &nbsp; catch (SQLException e) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}准备好的语句可以做很多事情,其中之一是处理有关如何在 SQL 查询中正确转义文字数据的混乱细节。在这种情况下,它们使您不必担心在插入的 Java 字符串周围放置单引号。语句还可以防止 SQL 注入等不良事件的发生。

侃侃尔雅

您没有在 catch 块中打印异常。您可以使用 e.printStackTrace();&nbsp;首先打印异常。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java