SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public int addThreads(Threads threads) {

int num=0;
String sql="insert into threads(fid,author,authorid,subject,dateline,lastPost,lastPoster,views,replies,displayOrder,highLight,digest,attachment,moderated) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
this.openConn();
try {
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,threads.getFid());
pstmt.setString(2, threads.getAuthor());
pstmt.setInt(3, threads.getAuthorid());
pstmt.setString(4,threads.getSubject());
pstmt.setString(5,df.format(threads.getDateline()));
pstmt.setString(6,df.format(threads.getLastPost()));
pstmt.setString(7,threads.getLastPoster());
pstmt.setInt(8,threads.getViews());
pstmt.setInt(9,threads.getReplies());
pstmt.setByte(10,threads.getDisplayOrder());
pstmt.setByte(11,threads.getHighLight());
pstmt.setByte(12,threads.getDigest());
pstmt.setByte(13,threads.getAttachment());
pstmt.setByte(14,threads.getModerated());
pstmt.executeUpdate();
pstmt=conn.prepareStatement("select @@identity as id");
rs=pstmt.executeQuery();
if(rs.next()){
num=rs.getInt("id");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeAll();
}
return num;
}
action中
       threads.setDateline(new Date());
 
        threads.setLastPost(new Date());
我在实现类中打印也日期,也精确到时分秒,但插入数据库中时分秒就变为00:00:00了,
我给它一个精确到时分秒的字符串,插入到数据库中的也变为00:00:00,为什么啊?帮帮忙吧?

解决方案 »

  1.   

    Threads threads,你的对象里面检查有值了么?
      

  2.   

    pstm.setDate(), 不是setString()
      

  3.   

    哥们,我以前也走过你这样的老路,太麻烦了,有更简单的.在数据库存中,字段直接定义成Date()类型,
    在插入语句中,日期那个字段,直接写成now(),就不要用?点位了.读取也一样,直接出来就是yyyy-mm-dd hh:mm:ss
      

  4.   

    字段类型是否是datetime 要是date应该就没有记录时间吧
      

  5.   

    带。0 的是因为设置时间的时候不是DATE类型。
    我记得当初我是to_date 了。
      

  6.   

    汗,你把 Connection 设成成员变量,迟早会让你吃苦头!
      

  7.   

    把数据库连接对象 Connection, PreparedStatement, ResultSet 这些设成成员变量的话,在并发环境下,将会有大量的连接得不到关闭,以至于运行效率越来越慢,直接最后连接池资源完全泄漏,无法获得连接而报错!
      

  8.   

    数据库字段需要设成 TIMESTAMP 类型的,否则不会有时分秒。
      

  9.   

    是可以的。如果直接插入日期的话,还要转换成sql的日期。有点麻烦。