简体   繁体   中英

I'm trying to insert data into MySQL through JFrame

I've been trying to insert data into a JFrame register form using MySQL database for the past day. But all I'm getting is errors that I don't know. Here my SQL code and the errors that are being displayed.

Can someone please help me fix this error?

 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");
        ;}
    }});

Stack trace

 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)
insertTableSQL = "INSERT INTO signup"+"(username,email,password,confirm) VALUES"+"(?,?,?,?)";

The point of using a PreparedStatement is that you can just use a simple string with the SQL and then replace the tokens with actual parameters.

In the SQL above you are using string concatenation which is unnecessary. Just use:

insertTableSQL = "INSERT INTO signup(username,email,password,confirm) VALUES(?,?,?,?)";

which makes it easier to read and less likely to make typing mistakes when added all the delimiters.

However, I doubt that is the problem.

Instead I see:

String password1 = new String(passwordField.getPassword());
String password2 = new String(passwordField_1.getPassword());

However you then use:

create.setString(3, password);
create.setString(4, password);

to replace the tokens. I would guess "password" is null?

Maybe you should be using "password1" and "password2"?

Edit:

You have a couple of problems. Take a look at your code:

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);
  1. You create two Statements
  2. You try to execute an "insert" SQL command as a query.

The solution is to:

  1. get rid of the Statement.
  2. get rid of the executeQuery.

If you want to do a query after the insert then you need to create a separate SQL query using a "Select" statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM