0

我不知道如何在 TextArea 字段中正确显示 2 个表中的数据。我在数据库表中添加了 2 行,但程序在 textArea 中显示 4 行而不是 2(重​​复),如果我添加 3 行程序显示 3x 第一行、3x 第二行、3x 第三行……等等。这是我的代码. 请以某种方式帮助我:(

    jTextArea1.setText(null);
    Connection connection=null;
    try
    {  
        String driverName="oracle.jdbc.driver.OracleDriver";
        Class.forName(driverName);

        String serverName = "193.2.139.xxx";
        String portNumber = "1521";
        String sid = "ers";
        String url = "jdbc:oracle:thin:@"+serverName+":"+portNumber+":"+sid;
        String username = "xxxxxxxx";
        String password = "xxxxxxxxxx";

        connection = DriverManager.getConnection(url, username, password);
        java.sql.Statement select = connection.createStatement();

        int id;
        String name; //column from table narocnik
        String surname; //column from table narocnik
        String address; //column from table narocnik
        String telephone; //column from table narocnik
        String date; //column from table prevoz
        String od; //column from table prevoz
        String v; //column from table prevoz
        String odhod; //column from table prevoz
        String cena; //column from table prevoz
        ResultSet rs1=select.executeQuery("select * from narocnik,prevoz");
        while(rs1.next()){
            id=rs1.getInt("id");
            name=rs1.getString("name");
            surname=rs1.getString("surname");
            address=rs1.getString("address");
            telephone=rs1.getString("telephone");
            date=rs1.getString("date");
            od=rs1.getString("od");
            v=rs1.getString("do");
            odhod=rs1.getString("odhod");
            cena=rs1.getString("cena");
            jTextArea1.append(name+", "+surname+", "+address+", "+telephone+", "+date+", "+od+", "+v+", "+odhod+", "+cena+"\n");
        }
        connection.close();       
    }
    catch(ClassNotFoundException e)
    {
        JOptionPane.showMessageDialog(this,"Ni gonilnika! "+e);
    }catch(SQLException e){
        JOptionPane.showMessageDialog(this,"Napaka pri povezavi! "+e);
    } 
4

1 回答 1

0

问题出在您的 SQL 查询中;

ResultSet rs1=select.executeQuery("select * from narocnik,prevoz");

如果你想连接两个表,你应该使用像内连接、左连接、右连接或外连接这样的连接。但是,当您在查询中只使用逗号 (,) 来连接两个表时,它将是外连接。它会给你多个结果;

用户内部加入喜欢 -

Select * from table1 inner join table2 on table1.id = table2.id;

希望它对你有用。

于 2013-08-08T15:44:57.027 回答