I created a new bean called client
with Id
, Name
, LastName
and Address
fields. I created the model and the view of course. My model returns a list of all the clients. And this one is working fine.
But I need a model where I can select only one specific client filtered by Id
. Can anybody tell me what I need to change (besides the SQL statement) inside this model so I get only one client according to filter (id) criteria from SQL?
{
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;
}
}
除了sql语句,您还意味着什么?您必须在查询中添加where子句,我认为没有其他可能的情况。
Well, I guess you already have a class with a method that you invoke to retrieve all the cliets, right?
Well, now add another method, but this time a method that receives the client Id as parameter:
public List<Client> getAllClients();
public Client getClientById(int clientId);
You will need a secondary SQL statement, since the logic in the first one is for retrieving all records. Something like:
"select clientId, clientName, ... from clients where clientId=?"
Using a JDBC PreparedStatement you can easily replace the ? for the actual parameter being received by your API.
You can also consider abstracting your mapping strategy so that you can use it for both methods:
class ClientMapper implements SqlMapper<Client> {
@Override
public Client map(ResultSet rs) throws SQLException {
Client client = new Client();
client.setId(rs.getInt("Id"));
client.setName(rs.getString("Name"));
client.setLastName(rs.getString("LastName"));
client.setAddress(rs.getString("Address"));
return client;
}
}
You can also have a ClientsMapper that uses this single client mapper to retrieve all clients.
class ClientsMapper implements SqlMapper<List<Client>>{
@Override
public List<Client> map(ResultSet rs){
List<Client> result = new ArrayList<>();
ClientMapper mapper = new ClientMapper();
while(rs.next()){
result.add(mapper.map(rs));
}
return result;
}
}
well you can have one more method that returns a single client based on the id you provide as argument.
public Client getClientById(int id){
//fetch the client data using the id
//make a local Client object
Client c = new Client();
//populate c based on the values you get from your database.
//return this local object of Client
return c;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.