測試 新增女神 方法報錯!
錯誤信息:
Exception in thread "main" 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 'values('小夏', 1, 22, '2016-05-13', '[email protected]', '13988886666', 'ad' at line 1
Hello 各位,以上是錯誤信息,晚上卡在這里了。按照信息提示,我檢查了 sql 語句,但還是找不到錯誤在哪里。請大家幫我檢查下!
以下為?DAO?部分的代碼:
public?class?GoddessDao?{
public?void?addGoddess(Goddess?g)?throws?Exception?{
Connection?conn?=?DBUtil.getConnection();
String?sql?=?"insert?into?imooc_goddess"?+?"("?+?"user_name,?sex,?age,?birthday,?email,?mobile,?"
+?"create_user,?create_date,?update_user,?update_date,?isdel"?+?")"?+?"values("
+?"?,??,??,??,??,??,??,?current_date(),??,?current_date(),??)";
java.sql.PreparedStatement?ptmt?=?conn.prepareStatement(sql);
ptmt.setString(1,?g.getUser_name());
ptmt.setInt(2,?g.getSex());
ptmt.setInt(3,?g.getAge());
ptmt.setDate(4,?new?Date(g.getBirthday().getTime()));
ptmt.setString(5,?g.getEmail());
ptmt.setString(6,?g.getMobile());
ptmt.setString(7,?g.getCreate_user());
ptmt.setString(8,?g.getUpdate_user());
ptmt.setInt(9,?g.getIsdel());
ptmt.execute();
}以下為?action?部分的代碼:
public?class?GoddessAction?{
public?static?void?main(String[]?args)?throws?Exception?{
GoddessDao?gd?=?new?GoddessDao();
Goddess?g1?=?new?Goddess();
g1.setUser_name("小夏");
g1.setSex(1);
g1.setAge(22);
g1.setBirthday(new?Date());
g1.setEmail("[email protected]");
g1.setMobile("13988886666");
g1.setCreate_user("admin");
g1.setUpdate_user("admin");
g1.setIsdel(1);
gd.addGoddess(g1);
2016-05-13
找到錯誤了:
DAO 部分的代碼 values 后面的左半括號寫成中文字符的括號了。。??匏?:(
???String?sql?=?"insert?into?imooc_goddess"?+?"("?+?"user_name,?sex,?age,?birthday,?email,?mobile,?" ????????????????+?"create_user,?create_date,?update_user,?update_date,?isdel"?+?")"?+?"values(???<-----看到沒,就是它,寫成中文括號了;低級小錯誤害死人" ????????????????+?"?,??,??,??,??,??,??,?current_date(),??,?current_date(),??)";