过去一天,我一直在尝试使用MySQL数据库将数据插入JFrame
注册表格中。但是我得到的只是我不知道的错误。这里是我的SQL代码和正在显示的错误。
有人可以帮我解决此错误吗?
JButton btnNewButton = new JButton("Sign Up");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String url = "jdbc:mysql://localhost:3306/login";
String user ="root";
String password ="toor";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
Statement state = conn.createStatement();
String username = textField.getText();
String email=textField_1.getText();
String password1 = new String(passwordField.getPassword());
String password2 = new String(passwordField_1.getPassword());
String insertTableSQL = "INSERT INTO signup"+"(username,email,password,confirm) VALUES"+"(?,?,?,?)";
PreparedStatement create = conn.prepareStatement(insertTableSQL);
create.setString(1, username);
create.setString(2, email);
create.setString(3, password);
create.setString(4, password);
create.executeUpdate(insertTableSQL);
state.executeQuery(insertTableSQL);
}catch(Exception arg0) {arg0.printStackTrace();
System.out.println("Success");
;}
}});
Success
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?)' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1355)
at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2128)
at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1264)
at SignUp$5.actionPerformed(SignUp.java:144)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:1)
insertTableSQL = "INSERT INTO signup"+"(username,email,password,confirm) VALUES"+"(?,?,?,?)";
使用PreparedStatement的要点是,您可以在SQL中使用简单的字符串,然后将标记替换为实际参数。
在上面的SQL中,您使用的是不必要的字符串连接。只需使用:
insertTableSQL = "INSERT INTO signup(username,email,password,confirm) VALUES(?,?,?,?)";
当添加所有定界符时,它更易于阅读,并且不太可能出现键入错误。
但是,我怀疑这是问题所在。
相反,我看到了:
String password1 = new String(passwordField.getPassword());
String password2 = new String(passwordField_1.getPassword());
但是您随后使用:
create.setString(3, password);
create.setString(4, password);
替换令牌。我猜“密码”为空?
也许您应该使用“ password1”和“ password2”?
编辑:
您有几个问题。查看您的代码:
Statement state = conn.createStatement();
String insertTableSQL = "INSERT INTO signup(username,email,password,confirm) VALUES(?,?,?,?)";
PreparedStatement create = conn.prepareStatement(insertTableSQL);
...
create.executeUpdate(insertTableSQL);
state.executeQuery(insertTableSQL);
解决方案是:
如果要在插入后进行查询,则需要使用“选择”语句创建一个单独的SQL查询。