/** * insert * * @param user * @return user The mistake inserted */ @Override public User insert(User user) throws UserException { Connection c = MysqlDAOFactory.createConnection(); if (user.getCreatedAt() == null) user.setCreatedAt(new Date().getTime()); PreparedStatement pstmt; try { pstmt = c.prepareStatement(INSERT); pstmt.setString(1, user.getEmail()); pstmt.setTimestamp(2, user.getCreateAtTime()); pstmt.executeUpdate(); ResultSet rset = pstmt.getGeneratedKeys(); rset.next(); Long idGenerated = rset.getLong(1); user.setId(idGenerated); pstmt.close(); c.close(); return user; } catch (SQLException e) { throw new UserException(e.getMessage()); } }
private User createUser(ResultSet rset) throws SQLException { User user = new User(rset.getString("email")); user.setCreatedAt(rset.getTimestamp("created_at").getTime()); user.setId(rset.getLong("id")); return user; }