在单独的行上打印数据库记录

8ljdwjyq  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(309)

我有一个jdbc程序,它从mysql数据库中获取记录并打印出结果。用户可以通过选择不同的复选框从数据库中选择想要的结果,以仅显示特定的结果。
下面是获取记录并打印出来的方法:

private void execute() throws SQLException {
    String query = "SELECT * FROM customers";
    ResultSet rs = stmt.executeQuery(query);
    String result = "";
    while (rs.next()) {
        if (cb1.isSelected()) {
            int custid = rs.getInt("custid");
            result += custid + " ";
        }
        if (cb2.isSelected()) {
            String name = rs.getString("name");
            result += name + " ";
        }
        if (cb3.isSelected()) {
            String address = rs.getString("address");
            result += address + " ";
        }
        if (cb4.isSelected()) {
            String city = rs.getString("city");
            result += city + " ";
        }
        if (cb5.isSelected()) {
            String state = rs.getString("state");
            result += state + " ";
        }
        if (cb6.isSelected()) {
            int zip = rs.getInt("zip");
            result += zip + " ";
        }
        // print the results
    }
    System.out.println(result);
    results.setText(result);

    stmt.close();
}

目前,如果我选中前三个复选框,我将得到输出:

1 Smith, Tim 12 Elm St 2 Jones, Tom 435 Oak Dr 3 Avery, Bill 623 Ash Ave 4 Kerr, Debra 1573 Yew Crt

但是,我想要的结果是:

1, Smith, Tim, 12 Elm St
2, Jones, Tom, 435 Oak Dr
3, Avery, Bill, 623 Ash Ave
4, Kerr, Debra, 1573 Yew Crt

有没有什么方法可以在数据库中的每条记录后面添加新行,或者在每条记录的项之间添加逗号?我是新的jdbc和mysql连接,所以任何帮助或提示表示感谢。

juud5qan

juud5qan1#

首先,我会使用 StringJoiner 收集元素。然后,我将消除许多局部临时变量。最后,我会用 println 在循环中和另一个循环中 StringJoiner 参加决赛 result . 比如,

private void execute() throws SQLException {
    String query = "SELECT * FROM customers";
    ResultSet rs = stmt.executeQuery(query);
    StringJoiner result = new StringJoiner(System.lineSeparator());
    while (rs.next()) {
        StringJoiner lineJoiner = new StringJoiner(", ");
        if (cb1.isSelected()) {
            lineJoiner.add(String.valueOf(rs.getInt("custid")));
        }
        if (cb2.isSelected()) {
            lineJoiner.add(rs.getString("name"));
        }
        if (cb3.isSelected()) {
            lineJoiner.add(rs.getString("address"));
        }
        if (cb4.isSelected()) {
            lineJoiner.add(rs.getString("city"));
        }
        if (cb5.isSelected()) {
            lineJoiner.add(rs.getString("state"));
        }
        if (cb6.isSelected()) {
            lineJoiner.add(String.valueOf(rs.getInt("zip")));
        }
        System.out.println(lineJoiner);
        result.add(lineJoiner.toString());
    }
    results.setText(result.toString());
    stmt.close();
}

你也可以做同样的事情 Collection (s) 比如,

String query = "SELECT * FROM customers";
ResultSet rs = stmt.executeQuery(query);
List<String> msg = new ArrayList<>();
while (rs.next()) {
    List<String> al = new ArrayList<>();
    if (cb1.isSelected()) {
        al.add(String.valueOf(rs.getInt("custid")));
    }
    if (cb2.isSelected()) {
        al.add(rs.getString("name"));
    }
    if (cb3.isSelected()) {
        al.add(rs.getString("address"));
    }
    if (cb4.isSelected()) {
        al.add(rs.getString("city"));
    }
    if (cb5.isSelected()) {
        al.add(rs.getString("state"));
    }
    if (cb6.isSelected()) {
        al.add(String.valueOf(rs.getInt("zip")));
    }
    String line = al.stream().collect(Collectors.joining(", "));
    System.out.println(line);
    msg.add(line);
}
results.setText(msg.stream().collect(Collectors.joining(System.lineSeparator())));
stmt.close();

喜欢你觉得可读性最好的。

0tdrvxhp

0tdrvxhp2#

您可以在while循环结束之前打印每个结果,然后它将在新行中打印每条记录。

private void execute() throws SQLException {
    String query = "SELECT * FROM customers";
    ResultSet rs = stmt.executeQuery(query);
    String result = "";
    String singleResult = "";
    while (rs.next()) {
        if (cb1.isSelected()) {
            int custid = rs.getInt("custid");
            singleResult += custid + " ";
        }
        if (cb2.isSelected()) {
            String name = rs.getString("name");
            singleResult += name + " ";
        }
        if (cb3.isSelected()) {
            String address = rs.getString("address");
            singleResult += address + " ";
        }
        if (cb4.isSelected()) {
            String city = rs.getString("city");
            singleResult += city + " ";
        }
        if (cb5.isSelected()) {
            String state = rs.getString("state");
            singleResult += state + " ";
        }
        if (cb6.isSelected()) {
            int zip = rs.getInt("zip");
            singleResult += zip + " ";
        }
        System.out.println(singleResult);
        result +=singleResult;
    }
    //System.out.println(result);
    results.setText(result);

    stmt.close();
}

或者可以在关闭while循环之前附加行分隔符

System.out.println(singleResult);
result +=singleResult;
result +="\n";

相关问题