使用jdbc - java应用程序将值插入mysql表

时间:2017-04-11 05:48:25

标签: java mysql jdbc

我正在尝试使用java实现用户注册,它总是返回错误,我的用户登录工作正常。但为什么会出现这个错误?

我已经实现了一个寄存器类,它创建连接并插入值。在actionperformed类中完成输入验证。

   private boolean register(String username,String email,String password) {
   try{           
   Class.forName("com.mysql.jdbc.Driver");  // MySQL database connection
   Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/erp?" + "user=root&password=");     
   PreparedStatement sql = (PreparedStatement) conn.prepareStatement("Insert into user(username,email,password) values(? , ?, ?)");
   sql.setString(1, username);
   sql.setString(2, email);
   sql.setString(3, password);
   ResultSet rs = sql.executeQuery();                        
   if(rs.next())            
       return true;    
   else
       return false;            
   }
   catch(Exception e){
   e.printStackTrace();
   return false;
   }       



private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if(jTextField1.getText().length()==0)  // Checking for empty field
        JOptionPane.showMessageDialog(null, "Insert Username");
    else if(jTextField2.getText().length()==0)  // Checking for empty field
        JOptionPane.showMessageDialog(null, "Insert E-mail address");
    else if(jPasswordField1.getText().length()==0)  // Checking for empty field
        JOptionPane.showMessageDialog(null, "Insert Password");
    else if(jPasswordField2.getText().length()==0)  // Checking for empty field
        JOptionPane.showMessageDialog(null, "Re-enter Password");
    else if(!jPasswordField1.getText().equals(jPasswordField2.getText()))  // Checking for equal passwords
        JOptionPane.showMessageDialog(null, "Password fields do not match");
    else{
        String user = jTextField1.getText();   // Collecting the input
        String email = jTextField2.getText(); // Collecting the input
        String pwd = jPasswordField1.getText(); // Collecting the input
            //String pwd = pass;  // converting from array to string
    if(register(user,email,pwd))
      JOptionPane.showMessageDialog(null, "Registered Successfully");        
    else
      JOptionPane.showMessageDialog(null, "Error");

    }        
}

3 个答案:

答案 0 :(得分:1)

您遇到问题,因为您使用PreparedStatement executeQuery方法。但这种方法适用于查询数据库。并且要更新数据库,您必须使用 PreparedStatement updateQuery方法。 简短的例子:

String insertTableSQL = "INSERT INTO DBUSER"
        + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
        + "(?,?,?,?)";
PreparedStatement preparedStatement = dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setInt(1, 11);
preparedStatement.setString(2, "mkyong");
preparedStatement.setString(3, "system");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
// execute insert SQL stetement
preparedStatement .executeUpdate();

答案 1 :(得分:1)

方法executeQuery()用于从数据库中检索数据,如“选择”查询/语句,但如果要运行“更新”,“删除”或“插入”查询,则必须使用executeUpdate()。use < / p>

ResultSet rs = sql.executeUpdate();

答案 2 :(得分:0)

在这个问题中你提到了你得到的错误类型

你应该只使用调试器,它将帮助你解决错误