0

This is my code. When I run it, I get error:

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

Can someone please help me?

form_()
{
    setSize(265,500);
    setVisible(true);
    setLayout(new FlowLayout());
    add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(l3);
    add(t3);
    add(l4);
    add(t5);
    add(l5);
    add(t4);
    add(b);
    b.addActionListener(this);

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==b) {
        int x = 0;
        String s1 = t1.getText();
        String s2 = t2.getText();
        String s3 = t3.getText();
        char[] c = t5.getPassword();
        String s5 = new String(c);
        String s4 = t4.getText();


        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useSSL=false", "******", "********");

            PreparedStatement ps = con.prepareStatement("INSERT INTO reg1 values(?,?,?,?,?");
            ps.setString(1, s1);
            ps.setString(2, s2);
            ps.setString(3, s3);
            ps.setString(4, s4);
            ps.setString(5, s5);
            int rs = ps.executeUpdate();
            x++;
            if(x>0)
            {
                JOptionPane.showMessageDialog(b,"Data saved Successfully");
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}
}

3 Answers 3

1

con.prepareStatement("INSERT INTO reg1 values(?,?,?,?,?"); this is the mistake in your code. you should try it this way con.prepareStatement("INSERT INTO reg1 values(?,?,?,?,?)");

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

1 Comment

It should be ("INSERT INTO reg1 values(?,?,?,?,?)"); isn't it?
1

I think you forget a parenthesis in "INSERT INTO reg1 values(?,?,?,?,?" it should be like that : "INSERT INTO reg1 values(?,?,?,?,?)"

Comments

0

You have messed up with the parathesis(SQL syntax). Add 1 ')' after last ?. which resulted in SQLSyntaxErrorException

I have updated the snippet:

PreparedStatement ps = con.prepareStatement("INSERT INTO reg1 values(?,?,?,?,?)");
            ps.setString(1, s1);
            ps.setString(2, s2);
            ps.setString(3, s3);
            ps.setString(4, s4);
            ps.setString(5, s5);

Comments

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.