当前位置: 代码迷 >> Java Web开发 >> 请教一个有关问题,请高手解答
  详细解决方案

请教一个有关问题,请高手解答

热度:11   发布时间:2016-04-17 13:50:07.0
请教一个问题,请高手解答
我要实现数据库操作的封装,写了一个方法
用的是   sqlserver   2000
数据库中usersexbrith,   updatedate为datatime类型

public   java.sql.Date   sqlDate,sqlUpdate;

public   void   update(Person   person)   throws   Exception   {

String   sql   =   "update   T_USERS     set   provincename=?,cityname=?,townname=?,usergroup=?,userid=?,userkind=?,userturename=?,usersex=?,usersexbrith=?,userpass=?   usercompass=?   passquestion=?,passanswer=?,contactman=?,useraddr=?,useremail=?,mobilenum=?,telnum=?,qqnum=?,userphoto=?,currstate=?,loginnum=?,updatedate=?,remarks=?   where   id=? ";
System.out.println(sql);
PreparedStatement   pstmt   =   null;
Connection   conn   =   null;
try   {
conn   =   new   ConnectionDB().getConnection();
pstmt   =   conn.prepareStatement(sql);
pstmt.setString(1,   person.getProvincename());
pstmt.setString(2,   person.getCityname());
pstmt.setString(3,   person.getTownname());
pstmt.setString(4,   person.getUsergroup());
pstmt.setString(5,   person.getUserid());
pstmt.setString(6,   person.getUserkind());
pstmt.setString(7,   person.getUserturename());
pstmt.setString(8,   person.getUsersex());


SimpleDateFormat   bartDateFormat   =   new   SimpleDateFormat( "yyyy-MM-dd ");
String   dateStringToParse   =   person.getUsersexbrith();
try   {
java.util.Date   date   =   bartDateFormat.parse(dateStringToParse);
  sqlDate   =   new   java.sql.Date(date.getTime());
System.out.println(sqlDate.getTime());
}   catch   (Exception   ex)   {
System.out.println(ex.getMessage());
}

pstmt.setDate(9,   sqlDate);
pstmt.setString(10,   person.getUserpass());
pstmt.setString(11,   person.getUsercompass());
pstmt.setString(12,   person.getPassquestion());
pstmt.setString(13,   person.getPassanswer());
pstmt.setString(14,   person.getContactman());
pstmt.setString(15,   person.getUseraddr());
pstmt.setString(16,   person.getUseremail());
pstmt.setString(17,   person.getMobilenum());
pstmt.setString(18,   person.getTelnum());
pstmt.setString(19,   person.getQqnum());
pstmt.setString(20,   person.getUserphoto());
pstmt.setString(21,   person.getCurrstate());
pstmt.setString(22,   person.getLoginnum());

SimpleDateFormat   df   =   new   SimpleDateFormat( "yyyy-MM-dd ");
String   stp   =   person.getUpdatedate();
try   {
java.util.Date   sdate   =   df.parse(stp);
  sqlUpdate   =   new   java.sql.Date(sdate.getTime());
System.out.println(sqlUpdate.getTime());
}   catch   (Exception   ex)   {
System.out.println(ex.getMessage());
}


pstmt.setDate(23,   sqlUpdate);

pstmt.setString(24,   person.getRemarks());

pstmt.setInt(25,   person.getId());
System.out.println( "update   person!!!!!!!!!!!!! ");
pstmt.executeUpdate();

//pstmt.close();
}   catch   (Exception   e)   {
throw   new   Exception( "操作出现异常 ");
}   finally   {
//conn.close();//   关闭数据库连接
}
  相关解决方案