1

I'm trying to insert data into mysql db from java GUI , however there is an exception stating

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: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

String sql = "INSERT INTO ugc VALUES (?,?,?,?,?)";

    try {
        pst=conn.prepareStatement(sql);
        pst.setString(1, univ_name.getText());
        pst.setString(2, reg_no.getText());
        pst.setString(3,affiliation);
        String naac_grade=naac.getSelectedItem().toString();
        pst.setString(4, naac_grade);
        pst.setString(5, country.getText());

        pst.executeUpdate(sql);
8
  • are you sure the sql excuted is the above as you showed because exception says you have a dot before last ? which is an error but in you code there is no dot Commented Aug 7, 2015 at 11:15
  • I have copy pasted the code Commented Aug 7, 2015 at 11:15
  • remove the parameters from pst.executeUpdate(sql); because you have already prepared the statement then there is no need to provide it again Commented Aug 7, 2015 at 11:17
  • Does you table has 5 columns ?? It not then this exception may arise.Add column name in the after "insert into ugc" Commented Aug 7, 2015 at 11:19
  • yes it has, and if i use executeQuery() it gives me another exception , stating data manipulations are not allowed Commented Aug 7, 2015 at 11:27

1 Answer 1

1

When executing the PreparedStatement use

pst.executeUpdate();

instead of

pst.executeUpdate(sql);       
Sign up to request clarification or add additional context in comments.

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.