Java打印数据库记录在不同的行上

时间:2018-04-29 03:24:02

标签: java mysql database string jdbc

我有一个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连接的新手,所以感谢任何帮助或提示。

2 个答案:

答案 0 :(得分:2)

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

.add{
position: absolute;
top: 0px;
display: table;
right: -26px;

}

或者您可以在关闭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();
}

答案 1 :(得分:1)

首先,我会使用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();

首选您找到的可读