Exemplo n.º 1
0
  public static Post addMessage(Post post, Connection con) {
    try {

      DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
      Date date = new Date();
      java.sql.Date sqlDate = new java.sql.Date(date.getTime());
      //            Connection con = Database.getConnection();
      PreparedStatement p =
          con.prepareStatement("INSERT INTO messages (message,created,author) VALUES(?,?,?)");

      p.setString(1, post.getContent());
      p.setDate(2, sqlDate);
      p.setString(3, post.getContent());
      boolean ok = p.execute();

      p =
          con.prepareStatement(
              "select id,created from  messages where message = ? and created = ? and author = ?");
      p.setString(1, post.getContent());
      p.setDate(2, sqlDate);
      p.setString(3, post.getContent());
      ResultSet resultSet = p.executeQuery();
      resultSet.next();
      post.setId(resultSet.getInt("id"));
      post.setCreated(resultSet.getDate("created"));
      resultSet.close();
      p.close();
      return post;
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException();
    }
  }