Reputation:
Help! I can't find out why I keep getting the error java.sql.SQLSyntaxException: 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 'by)values ('19','Bob','Marquez','Secretary'......) at line 1 Here is my sql query with the code.
String value = lbl_emp.getText();
try{
String sql= "insert into thepayroll
(emp_id, first_name, last_name, job_title, basic_salary,
rate, overtime, overtime_hrs, "+
"a_vale, bonus, a_other, a_reason, total_allowance, sss,
pagibig, philhealth, d_vale, absence, d_other, " +
"d_reason, total_deduction, total_salary, by)
values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'"+value+"')";
pst = conn.prepareStatement(sql);
pst.setString(1,txt_empid.getText());
pst.setString(2,txt_firstname.getText());
pst.setString(3,txt_lastname.getText());
pst.setString(4,txt_job.getText());
pst.setString(5,txt_salary.getText());
pst.setString(6,txt_drate.getText());
pst.setString(7,lbl_overtime.getText());
pst.setString(8,txt_overtime.getText());
pst.setString(9,lbl_avale.getText());
pst.setString(10,txt_bonus.getText());
pst.setString(11,txt_aother.getText());
pst.setString(12,txt_areason.getText());
pst.setString(13,lbl_atotal.getText());
pst.setString(14,txt_sss.getText());
pst.setString(15,txt_pagibig.getText());
pst.setString(16,txt_philhealth.getText());
pst.setString(17,txt_dvale.getText());
pst.setString(18,lbl_absence.getText());
pst.setString(19,lbl_dother.getText());
pst.setString(20,txt_dreason.getText());
pst.setString(21,lbl_dtotal.getText());
pst.setString(22,lbl_stotal.getText());
pst.execute();
JOptionPane.showMessageDialog(null,"Data is saved successfully");
} catch (Exception e){
JOptionPane.showMessageDialog(null,e);
} finally {
try{
rs.close();
pst.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
}
Upvotes: 0
Views: 50
Reputation: 562250
BY
is a reserved keyword in MySQL. In other words, it has importance in the SQL parser, so using it as a column identifier confuses MySQL's parser.
To use a reserved keyword as a column identifier, you must delimit it in back-ticks:
INSERT INTO thepayroll (..., `by`) VALUES (...)
You need to do this with all reserved keywords. See the link I gave above. If the keyword in that documentation page has "(R)" next to it, it's a reserved keyword and must be delimited.
An alternative is to name your columns something else, so they don't conflict with any reserved keywords.
Upvotes: 1