问答详情
源自:2-5 JDBC实战---搭建模型层 Ⅲ

帮忙查找一下错误

    //修改功能
public void updateGodess(Goddess g) throws Exception{
    Connection conn =DBUtil.getConnection();
    String sql =""
            + " update imooc_goddess "+
                    " (user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?, "
                    +" update_user=?,update_date=current_date(),isdel=?) "
                    +" where id =? ";
    PreparedStatement ptmp =  conn.prepareStatement(sql);
    ptmp.setString(1, g.getUser_name());
    ptmp.setInt(2,g.getSex());
    ptmp.setInt(3, g.getAge());
    ptmp.setDate(4, new Date(g.getBirthday().getTime()));
    ptmp.setString(5, g.getEmail());
    ptmp.setString(6, g.getMobile());
    ptmp.setString(7, g.getUpdate_user());
    ptmp.setInt(8, g.getIsdel());
    ptmp.setInt(9, g.getId());
       
    ptmp.execute();
}

测试代码

Goddess g1 = new Goddess();
    g1.setUser_name("小月");
    g1.setAge(22);
    g1.setSex(1);
    g1.setBirthday(new Date());
    g1.setEmail("xiaoxai@qq.com");
    g1.setMobile("1234388888");
    g1.setUpdate_user("ADMIN");
    g1.setIsdel(1);
    g1.setId(3);
    //g.addGoddess(g1);
    g.updateGodess(g1);

抛出异常

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 '(user_name='小月',sex=1,age=22,birthday='2016-07-05',email='xiaoxai@qq.com',mo' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

提问者:书子 2016-07-05 17:11

个回答

  • EKKOY
    2017-03-06 10:03:14

    +" update_user=?,update_date=current_date(),isdel=?) "你这句多了个)

  • 安亦殊
    2016-07-06 13:06:24

    你的SQL语句那里写错了,换成下面这样  update 表名  set 字段1="", 字段2=""     where  成立的条件

    String sql=""+
    		      " update imooc_goddess"+
    		      " set user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?,"+
    		      " update_user=?,update_date=current_date(),isdel=?"+
    		      " where id =?";