插入過程中Mysql的日期欄的錯誤,我想用JDBC插入數據,每當我執行程序就說明我一些MySQL錯誤:從Java
Insert語句:
String sql = "INSERT into books(name, isbn, author, category, desc, published) VALUES('"+name+"','"+isbn+"','"+author+"','"+category+"', '"+desc+"','"+book_published+"')";
我試圖將字符串轉換使用到這裏日期:
String yr = year.getSelectedItem().toString();
String mn = month.getSelectedItem().toString();
String dy = day.getSelectedItem().toString();
String book_date = yr+"-"+mn+"-"+dy;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd",
Locale.ENGLISH);
try{
Date book_published = df.parse(book_date);
}catch(...){...}
,它顯示了我錯誤如:
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 'desc, published) VALUES('skd flakj','klsdjf askj','kl jasdklfj kl','kls djfklj f' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
請幫我弄清楚這裏有什麼問題。
您的一個值是否有一個流浪的單引號?通過使用參數綁定而不是直接在SQL字符串中連接值,可以避免很多麻煩。 – sstan
[Using Prepared Statements](http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html) – MadProgrammer