1、从结果集中取得日期部分
resultSet.getDate(); --2013-01-07
2、从结果集中取得时间部分
resultSet.getTime() --22:08:09
3、从结果集中同时得到日期和时间
resultSet.getTimestamp(); --2013-01-07 23:08:09
@Override public List<User> findAll() { List<User> list = new ArrayList<User>(); Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = JdbcUtil.getConnection(); String sql = "select * from [user]"; stmt = con.prepareStatement(sql); rs = stmt.executeQuery(); while(rs.next()) { //精确到秒 yyyy-MM-dd HH:mm:ss User u = new User(rs.getInt("id"), rs.getString("username"),rs.getString("password"),rs.getTimestamp("createtime")); //精确到秒 yyyy-MM-dd //User u = new User(rs.getInt("id"), rs.getString("username"),rs.getString("password"),rs.getDate("createtime")); list.add(u); } return list; } catch (Exception e) { e.printStackTrace(); } finally { JdbcUtil.close(rs, stmt, con); } return null; } @Override public void add(User user) { Connection con = null; PreparedStatement stmt = null; ResultSet rs = null; try { con = JdbcUtil.getConnection(); String sql = "insert into [user](username,password,createtime) values(?,?,?)"; stmt= con.prepareStatement(sql); stmt.setString(1, user.getUsername()); stmt.setString(2,user.getPassword()); //精确到秒 yyyy-MM-dd HH:mm:ss //stmt.setTimestamp(3, new java.sql.Timestamp((new Date()).getTime())); //下面精确到 日 yyyy-MM-dd stmt.setDate(3, new java.sql.Date(new Date().getTime())); stmt.execute(); } catch (Exception e) { e.printStackTrace(); }finally { JdbcUtil.close(rs, stmt, con); } }