在Java输出中插入空行

时间:2016-03-18 15:37:28

标签: java mysql

我有一些代码要打印出与学生一起的整个课程列表。我希望这个列表按class_id排序,我想在每个不同的类之间有一个空格。例如:

100 Math Jon Doe
100 Math Tina Lee
100 Math Sally

122 Science Tom Edison
122 Science Jon Frankin

现在代码正确打印出来,除了没有空格分隔类。

我知道错误在这里:

while(!"class_id".equals("class_id")) {
                    System.out.println("\n");

                }

不确定我做错了什么。

static void listAllClasses() {
        System.out.println("\nClasses With Enrollment\n");
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "", "");
            String totalEnroll = ("SELECT * FROM ClassSelector.student_x_class ORDER BY class_id");
            myStmt = con.prepareStatement(totalEnroll);

            rs = myStmt.executeQuery(totalEnroll);
            while (rs.next()) {
                String totalEnrollment = rs.getString("class_id") + "\t" + rs.getString("class_name") + "\t"  + rs.getString("student_id") + "\t" + rs.getString("student_name");
                while(!"class_id".equals("class_id")) {
                    System.out.println("\n");

                }
                System.out.println(totalEnrollment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

3 个答案:

答案 0 :(得分:1)

试试这个:

static void listAllClasses() {
        String currentClassId ="";
        String oldClassId = "";
        System.out.println("\nClasses With Enrollment\n");
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ClassSelector?autoReconnect=true&useSSL=false", "", "");
            String totalEnroll = ("SELECT * FROM ClassSelector.student_x_class ORDER BY class_id");
            myStmt = con.prepareStatement(totalEnroll);

            rs = myStmt.executeQuery(totalEnroll);
            while (rs.next()) {
                currentClassId = rs.getString("class_id");
                String totalEnrollment = rs.getString("class_id") + "\t" + rs.getString("class_name") + "\t"  + rs.getString("student_id") + "\t" + rs.getString("student_name");
                while(currentClassId != null && oldClassId != null && !currentClassId.equals(oldClassId)) {
                    System.out.println("\n");

                }
                oldClassId = currentClassId;
                System.out.println(totalEnrollment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:0)

尝试:

String lastClass = "";
while (rs.next()) {
        String currentClass = rs.getString("class_id");
        String totalEnrollment = rs.getString("class_id") + "\t" + rs.getString("class_name") + "\t"  + rs.getString("student_id") + "\t" + rs.getString("student_name");
        if(!( currentClass.equals(lastClass) || "",equals( lastClass ) )) {
            System.out.println("\n");
        }
        lastClass = currentClass;
        System.out.println(totalEnrollment);

答案 2 :(得分:0)

!"class_id".equals("class_id")永远不会发生。

你想要的可能是什么

        rs = myStmt.executeQuery(totalEnroll);
        String prevClassID = null;
        while (rs.next()) {
            String curClassID = rs.getString("class_id");
            String totalEnrollment = curClassID + "\t" + rs.getString("class_name") + "\t"  + rs.getString("student_id") + "\t" + rs.getString("student_name");
            if (prevClassID != null && !curClassID.equals(prevClassID)) {
                System.out.print("\n");
            }
            System.out.println(totalEnrollment);
            prevClassID = curClassID;
        }