package com.cin.test.util;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.cin.dto.InvitationDTO;
import com.cin.dto.UserDTO;
public class InvitationTable extends Table {
InvitationTable(Connection connection){
super("invitation", connection);
this.connection = connection;
}
public InvitationDTO get(int id) throws SQLException{
Statement stmt = connection.createStatement();
try{
stmt.execute("select ID, CUSTOMER_SSN, AGENT_SSN, INVITE_DATE from invitation where id="+id);
ResultSet rs = stmt.getResultSet();
if( rs.next() ){
assert rs.getInt(1) == id;
UserDTO customer = new UserDTO();
customer.setSsn(rs.getInt(2));
UserDTO agent = new UserDTO();
agent.setSsn(rs.getInt(3));
InvitationDTO inv = new InvitationDTO(id, agent, customer, rs.getDate(4));
return inv;
} else {
return null;
}
} finally {
stmt.close();
}
}
public InvitationDTO getNewest() throws SQLException{
Statement stmt = connection.createStatement();
stmt.execute(
"select ID "+
"FROM invitation "+
"ORDER BY INVITE_DATE DESC "+
"FETCH FIRST ROW ONLY ");
ResultSet rs = stmt.getResultSet();
if( rs.next() ){
return get(rs.getInt(1));
} else {
return null;
}
}
@Override
public void init() throws SQLException {
}
}