Added getAll method to ORM

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
Jim Martens 2017-04-18 19:22:41 +02:00
parent b3fe215dcd
commit 2d1c4a2c75
1 changed files with 37 additions and 0 deletions

View File

@ -3,7 +3,9 @@ package de.dis2017.data.db;
import de.dis2017.data.EstateAgent;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -23,6 +25,41 @@ public class ORM {
_agents = new HashMap<>();
}
/**
* Loads all estate agents from the database and returns a list with them.
*
* @return a list of estate agents
*/
public List<EstateAgent> getAll() {
List<EstateAgent> agents = new ArrayList<>();
try {
// create query
String selectSQL = "SELECT * FROM ESTATEAGENT";
PreparedStatement pstmt = _connection.prepareStatement(selectSQL);
// execute query
ResultSet rs = pstmt.executeQuery();
EstateAgent agent;
while (rs.next()) {
agent = new EstateAgent();
agent.setId(rs.getInt("ID"));
agent.setName(rs.getString("name"));
agent.setAddress(rs.getString("address"));
agent.setLogin(rs.getString("login"));
agent.setPassword(rs.getString("password"));
_agents.put(agent.getId(), agent);
agents.add(agent);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return agents;
}
/**
* Loads the estate agent with the given ID from database and returns the corresponding object.
*