[英]I can't find out what's wrong with my sql syntax
Help.帮助。 I can't find out why I keep getting the error java.sql:SQLSyntaxException;我不知道为什么我不断收到错误 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.您的 SQL 语法有错误,请查看与您的 MySQL 服务器版本相对应的手册,以获取正确的语法以使用接近 'by) 值('19'、'Bob'、'Marquez'.'Secretary'.... ..) 在第 1 行这是我的 sql 查询代码。
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);
}
}
}
BY
is a reserved keyword in MySQL . BY
是MySQL 中的保留关键字。 In other words, it has importance in the SQL parser, so using it as a column identifier confuses MySQL's parser.换句话说,它在 SQL 解析器中具有重要性,因此将其用作列标识符会使 MySQL 的解析器感到困惑。
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.如果该文档页面中的关键字旁边有“(R)”,则它是保留关键字,必须加以分隔。
An alternative is to name your columns something else, so they don't conflict with any reserved keywords.另一种方法是将您的列命名为其他名称,这样它们就不会与任何保留关键字冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.