为了账号安全,请及时绑定邮箱和手机立即绑定

如何重新构建方法以返回客户列表?

如何重新构建方法以返回客户列表?

慕斯709654 2021-07-14 02:06:04
我需要重新构建一些方法来返回客户端列表。我需要那个,因为在那一刻我无法编写任何单元测试。这是我负责将客户端添加到数据库的方法。正如你所看到的,像:name, surname... 这样的值是从已经创建的客户端对象中获取的。 汽车租赁SQL数据库@Overridepublic void addClient(Client client) throws SQLException {    preparedStatement = connection.prepareStatement("insert into client" + "(namee, surname, street,houseNumber,city,peselNumber,rentDate, clientNumber)" + "values(?,?,?,?,?,?,?,?)");    preparedStatement.setString(1, client.getName());    preparedStatement.setString(2, client.getSurname());    preparedStatement.setString(3, client.getStreet());    preparedStatement.setInt(4, client.getHouseNumber());    preparedStatement.setString(5, client.getCity());    preparedStatement.setLong(6, client.getPeselNumber());    preparedStatement.setString(7, client.getRentDate());    preparedStatement.setInt(8, client.getClientNumber());    preparedStatement.executeUpdate();}这是我负责在屏幕上显示所有客户端的方法: CarRentalSQLDatabase@Overridepublic void populateTableViewClients() throws SQLException {    String sql = "SELECT * FROM `client`";    result = statement.executeQuery(sql);    while (result.next()) {        String namee = result.getString("namee");        String surname = result.getString("surname");        String street = result.getString("street");        int houseNumber = result.getInt("houseNumber");        long peselNumber = result.getLong("peselNumber");        String rentDate = result.getString("rentDate");        System.out.println("----------------------------");        System.out.printf("Name:" + namee + "\nSurname:" + surname + "\nStreet:" + street + "\nNumber of house:" + houseNumber + "\nPesel number:" + peselNumber + "\nDate of rental:" + rentDate + "\n");        System.out.println("----------------------------");    }}我想构建这样的东西:public List<Client> getAllCustomers() throws SQLException {    List<Client> listOfClients = new ArrayList<Client>();    String sql = "SELECT * FROM `client`";    result = statement.executeQuery(sql);    //....    return listOfClients;
查看完整描述

1 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

public List<Client> getAllCustomers() throws SQLException {

    List<Client> listOfClients = new ArrayList<Client>();


    String sql = "SELECT * FROM `client`";

    result = statement.executeQuery(sql);


    while (result.next()) {

        Client client = new Client();

        client.setName(result.getString("namee"));

        client.setSurname(result.getString("surname"));

        // ... and so on

        listOfClients.add(client);

    }


    return listOfClients;

}


查看完整回答
反对 回复 2021-07-14
  • 1 回答
  • 0 关注
  • 128 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信