0

I need help with this code. I kept having this kind of error but I don't know the reason.

        String nam = name.getText();
        String number = num.getText();
        String pass = password.getText();
        String mail = email.getText();
        try{
            Statement s = db.mycon().createStatement();
            s.executeUpdate("INSERT INTO user (full_name, phone_number, password, email) VALUES ('"+nam+"','"+number+"','"+pass+"','"+mail+"')");
            Success SuccessFrame = new Success();
            SuccessFrame.setVisible(true);
            SuccessFrame.pack();
            SuccessFrame.setLocationRelativeTo(null);
            this.dispose();
        } catch (Exception e){
            System.out.println(e);
        }
        
        name.setText("");
        num.setText("");
        password.setText("");
        email.setText("");

I don't see anymore mistakes in my value of column but still I get this error whenever I run the program. Help me to resolve this problem please.

2
  • 3
    Make it a lot easier on yourself by using PreparedStatement::setString etc. Commented May 3, 2024 at 10:25
  • Does any of the values you concatenate contain apostrophes and commas? But as g00se says, use a PreparedStatement with placeholders (parameters) and explicitly set the values with its setXXX methods. Your current code is extremely vulnerable to SQL injection. Commented May 3, 2024 at 13:00

1 Answer 1

0

Rather than creating a statement by yourself, use PreparedStatement. I have found the below stackoverflow chain which guides for using the same.

How to use PreparedStatement with JDBC

Sign up to request clarification or add additional context in comments.

1 Comment

You could improve your answer by providing a small example. Note that you can edit your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.