0

I have java code which connect to MySQL and retrive data. I have following code and it always give SQLSyntaxErrorException and says there is syntax error in line 1. I cant understand why. Please help me. I can sure the names are correct.


private void saveDetails(int slct) throws SQLException {
 if(slct == ADD_NEW_RECORD) {
 String query = "INSERT INTO emp(?,?,?,?,?,?,?,?,?,?,?,?,?,?) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
 PreparedStatement st = Main.db.con.prepareStatement(query);
 st.setString(1, "epfno");
 st.setString(2, "fname");
 st.setString(3, "lname");
 st.setString(4, "sex");
 st.setString(5, "nid");
 st.setString(6, "address");
 st.setString(7, "birthday");
 st.setString(8, "position");
 st.setString(9, "tpno");
 st.setString(10, "fathername");
 st.setString(11, "mothername");
 st.setString(12, "m_status");
 st.setString(13, "comments");
 st.setString(14, "photo_id");
 st.setInt(15, emp.epfno);
 st.setString(16, emp.fname);
 st.setString(17, emp.lname);
 st.setInt(18, emp.sex);
 st.setString(19, String.copyValueOf(emp.nid));
 st.setString(20, emp.address);
 st.setDate(21, emp.birthday);
 st.setString(22, emp.position);
 st.setString(23, emp.tpno);
 st.setString(24, emp.fathername);
 st.setString(25, emp.mothername);
 st.setBoolean(26, emp.m_status);
 st.setString(27, emp.comments);
 st.setString(28, emp.photo_id);
 st.execute();
 }
 }
Marius Burz
4,6452 gold badges20 silver badges28 bronze badges
asked Sep 1, 2011 at 16:50
1
  • Please add the error stack trace or log output Commented Sep 1, 2011 at 16:52

4 Answers 4

7

You're trying to set the field names as parameters - you can't do that. You can only specify values as parameters. So basically your first 14 setString calls should go away, and you should put those field names in the SQL:

String query = "INSERT INTO emp(epfno, fname, lname, sex, nid, address, "
 + "birthday, position, tpno, fathername, mothername, m_status, "
 + "comments, photo_id) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
st.setInt(1, emp.epfno);
st.setString(2, emp.fname);
st.setString(3, emp.lname);
// etc
answered Sep 1, 2011 at 16:52
Sign up to request clarification or add additional context in comments.

Comments

3

You cannot parameterize the name of the columns, so your SQL needs to look something like this:
INSERT INTO emp(epfno, fname, ...) VALUES (?, ?, ...)

answered Sep 1, 2011 at 16:53

Comments

3

You cannot specify column names as parameters. Try spelling them out in your query:

INSERT INTO emp(epfno, fname, lname,...

and getting rid of the first half of the st.setXXX calls.

answered Sep 1, 2011 at 16:53

Comments

3

You can't pass in column names as ? parameters. You would need to name the columns in your query and only pass the values in as parameters

answered Sep 1, 2011 at 16:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.