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

我需要重新构建一些方法来返回客户端列表。我需要那个,因为在那一刻我无法编写任何单元测试。这是我负责将客户端添加到数据库的方法。正如你所看到的,像:name, surname... 这样的值是从已经创建的客户端对象中获取的。 汽车租赁SQL数据库


@Override

public 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


@Override

public 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;

慕斯709654
浏览 129回答 1
1回答

HUH函数

public List<Client> getAllCustomers() throws SQLException {&nbsp; &nbsp; List<Client> listOfClients = new ArrayList<Client>();&nbsp; &nbsp; String sql = "SELECT * FROM `client`";&nbsp; &nbsp; result = statement.executeQuery(sql);&nbsp; &nbsp; while (result.next()) {&nbsp; &nbsp; &nbsp; &nbsp; Client client = new Client();&nbsp; &nbsp; &nbsp; &nbsp; client.setName(result.getString("namee"));&nbsp; &nbsp; &nbsp; &nbsp; client.setSurname(result.getString("surname"));&nbsp; &nbsp; &nbsp; &nbsp; // ... and so on&nbsp; &nbsp; &nbsp; &nbsp; listOfClients.add(client);&nbsp; &nbsp; }&nbsp; &nbsp; return listOfClients;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java