0

I'm trying to import data into a MySQL database using Java and I'm getting this error:

insert into employee(emp_id, name, dob, email, phone_no, account_number, wage_type, wage_rate, admin_id)values('20002','A B',STR_TO_DATE('20/03/10','%d/%m/%y'),'CGUltimateno1@gmail.com','02194024245','934546','Monthly',''2000,20002')'
SQL State: 42000
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 '2000,20002')'' at line 1

Here's the inserting code:

   query = "insert into employee(emp_id, name, dob, email, phone_no, account_number, wage_type, wage_rate, admin_id)" + 
"values('" + emp.getEmp_id() + "','" + emp.getName() + "','" + "STR_TO_DATE('" + emp.getDob() + "','%d/%m/%y')" + "','" + emp.getEmail() + "','" + emp.getPhone_no() + "','" +  emp.getAccount_number() + "','" + emp.getWage_type() + "','" + emp.getWage_rate() + "',''" + emp.getAdmin_id() + "')'";

I tried to play around removing the extra ' and " but no luck yet I'm sure the issue is in it

CGUltimateno
  • 19
  • 1
  • 5
  • It's alright I fixed it. turns out as I said my issue is in the ' and ". Here's the corrected version: "values('" + emp.getEmp_id() + "','" + emp.getName() + "'," + "STR_TO_DATE('" + emp.getDob() + "','%d/%m/%y')" + ",'" + emp.getEmail() +"','" + emp.getPhone_no() + "','" + emp.getAccount_number() + "','" + emp.getWage_type() + "'," +Integer.toString(emp.getWage_rate()) + ",'" + emp.getAdmin_id() + "')"; – CGUltimateno Dec 12 '22 at 20:30
  • I recommend you use query parameters instead of string concatenation. You'll find it easier to avoid these sorts of syntax errors. – Bill Karwin Dec 12 '22 at 21:09

0 Answers0