Skip to content

Commit

Permalink
MessageDAO insert message method
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsundin committed Jan 30, 2024
1 parent 44017ae commit cf4ec6f
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/DAO/MessageDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package DAO;

import Model.Message;
import Util.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.List;
import java.util.ArrayList;

public class MessageDAO {

public Message insertMessage(int posted_by, String message_text, long time_posted_epoch) {
try {
Connection conn = ConnectionUtil.getConnection();
String sql = "INSERT INTO message (posted_by, message_text, time_posted_epoch) "
+ "VALUES (?, ?, ?)";

PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, posted_by);
ps.setString(2, message_text);
ps.setLong(3, time_posted_epoch);

ps.executeUpdate();

ResultSet pkeyResultSet = ps.getGeneratedKeys();
while (pkeyResultSet.next()) {
Message message = new Message();
message.setMessage_id(pkeyResultSet.getInt(1));
message.setPosted_by(posted_by);
message.setMessage_text(message_text);
message.setTime_posted_epoch(time_posted_epoch);
return message;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

}

0 comments on commit cf4ec6f

Please sign in to comment.