使用自联接将来自同一数据库colomn的数据显示为2个不同的jtable colomns

时间:2018-01-05 13:44:33

标签: java mysql self-join

我希望使用语句中的自连接将来自同一数据库colomn的数据显示为2个不同的jtable colomn。我很确定我的问题在这里,但我不知道如何解决它:

    while (rs.next()) {
        String d = rs.getString("a.nrzbor");
        String e = rs.getString("b.nrzbor");
        model.addRow(new Object[]{d, e});
        jTable5.setModel(model);
    }

以下是完整代码:

    String de_la = introducereOras1.getText();
    String la = introducereOras2.getText();
    DefaultTableModel model = new DefaultTableModel(new String[]{"nrzbor1", "nrzbor2"}, 0);

    String url = "jdbc:mysql://localhost:3306/aeroport";
    String user = "root";
    String password = "";

    PreparedStatement myStmt = null;
    ResultSet rs = null;

    try{
        Connection myConn = DriverManager.getConnection(url, user, password);
        myStmt = myConn.prepareStatement("SELECT a.nrzbor , b.nrzbor" +
                                    "FROM Zboruri a,Zboruri b\n" +  
                                    "WHERE (a.de_la = ? AND b.de_la = ?) AND (a.la = ? AND b.la = ?);");
        myStmt.setString(1, de_la);
        myStmt.setString(2, la);
        myStmt.setString(3, la);
        myStmt.setString(4, de_la);
        rs = myStmt.executeQuery();
        while (rs.next()) {
            String d = rs.getString("a.nrzbor");
            String e = rs.getString("b.nrzbor");
            model.addRow(new Object[]{d, e});
            jTable5.setModel(model);
        }
    }
    catch(Exception e){
        System.err.println("Got an exception!");
        System.err.println(e.getMessage());
    }

有人能帮帮我吗?谢谢!

0 个答案:

没有答案