我创建了一个名为、 和client
字段Id
的新Name
bean 。我当然创建了模型和视图。我的模型返回所有客户的列表。这个工作正常。LastName
Address
但我需要一个模型,我只能选择一个由Id
. 谁能告诉我我需要在这个模型中更改什么(除了 SQL 语句),以便我根据 SQL 的过滤器(id)标准只获得一个客户端?
{
Connection connection = getDatabaseConnection();
request.setAttribute("clientList", getClientList(connection));
closeDatabaseConnection(connection);
}
private ArrayList<Client> getClientList(Connection con)
{
String sqlstr = "SELECT * FROM Clients";
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList<Client> clients = new ArrayList<Client>();
try
{
stmt = con.prepareStatement(sqlStr);
rs = stmt.executeQuery();
while (rs.next())
{
Client client = new Client();
client.setId(rs.getInt("Id"));
client.setName(rs.getString("Name"));
client.setLastName(rs.getString("LastName"));
client.setAddress(rs.getString("Address"));
clients.add(client);
}
rs.close();
stmt.close();
}
catch (SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
return clients;
}
}