public static List<Message> getMessagesByUser(User user) { List<Message> messages = new ArrayList<Message>(); ResultSet resultSet; Message message; String text, hash; int id; boolean valid = true; String sql = "SELECT * FROM mail WHERE userid = " + user.getId() + " ;"; try { resultSet = getStatement().executeQuery(sql); while (resultSet.next()) { id = resultSet.getInt("mailid"); text = resultSet.getString("message"); hash = resultSet.getString("hash"); message = Message.create(id, text, text.length(), hash, valid); messages.add(message); } getStatement().close(); } catch (SQLException e) { e.printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } return messages; }
@Deprecated public static void addNewMessageToUser(User user, Message message) { String sql = "SELECT MAX(mailid) as 'maxid' FROM mail;"; ResultSet resultSet = null; int number = 10000; try { resultSet = getStatement().executeQuery(sql); resultSet.next(); number = resultSet.getInt("maxid"); number++; sql = "INSERT INTO mail (mailid,userid,hash,message) values(" + number + "," + user.getId() + ",'" + message.getHash() + "','" + message.getContent().replace("'", "\"") + "');"; getStatement().execute(sql); resultSet.close(); getStatement().close(); } catch (SQLException e) { e.printStackTrace(); } }
public static void saveMail(User user, Message message) { ResultSet resultSet = null; try { String sql = "INSERT INTO mail (mailid,userid,hash,message) values(" + message.getId() + "," + user.getId() + ",'" + message.getHash() + "','" + message.getContent() + "');"; getStatement().execute(sql); resultSet.close(); getStatement().close(); } catch (SQLException e) { e.printStackTrace(); } }
public static void addMessagesToUser(User user) { List<Message> messages = MailProvider.getMessagesByUser(user); user.setUserMails(messages); }