码农_鑫森淼焱垚
2015-10-20 19:46
public void updateGoddess(Goddess g) { Connection conn = DBUtil.getConnection(); String sql = "" + "update imooc_goddess " + " set user_name=?,sex=?,age=?,birthday=?,email=?,mobile=?," + " update_user=?,update_date=current_date(),isdel=? " + " where id=?"; try { // 预编译SQL PreparedStatement pst = conn.prepareStatement(sql); //g = new Goddess(); 此处不能创建新的对象(问题根源) pst.setString(1, g.getUser_name()); pst.setInt(2, g.getSex()); pst.setInt(3, g.getAge()); pst.setDate(4, new Date(g.getBirthday().getTime())); pst.setString(5, g.getEmail()); pst.setString(6, g.getMobile()); pst.setString(7, g.getUpdate_user()); pst.setInt(8, g.getIsdel()); pst.setInt(9, g.getId()); pst.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public class GoddessAction { public static void main(String[] args) { GoddessDao g = new GoddessDao(); // List<Goddess> gList = g.query(); // for (Goddess goddess : gList) { // System.out.println(goddess.getUser_name() + "," + goddess.getSex() // + "," + goddess.getAge() + "," + goddess.getBirthday()); // } Goddess g1 = new Goddess(); g1.setUser_name("小美"); g1.setSex(1); g1.setAge(20); g1.setBirthday(new Date()); g1.setEmail("www.xiaobai@163.com"); g1.setMobile("18812341234"); g1.setCreate_user("localhost"); g1.setUpdate_user("localhost"); g1.setIsdel(1); g1.setId(3); g.updateGoddess(g1); } }
在更新birthday时出现空指针异常
Exception in thread "main" java.lang.NullPointerException at com.imooc.dao.GoddessDao.updateGoddess(GoddessDao.java:73) at com.imooc.action.GoddessAction.main(GoddessAction.java:39)
你在action辛辛苦苦创一个对象,传到dao,然后再把它设置为空,对象里的东西都没了,更新时空指针了
JDBC之 “ 对岸的女孩看过来”
99324 学习 · 856 问题
相似问题