我不知道如何在TextArea字段中正确显示2个表中的数据。我在数据库表中添加了2行,但程序在textArea中显示4行而不是2(重复),如果我添加3行程序显示3xfirst row,3x second row,3x third row ...等。这是我的代码。请以某种方式帮助我:(
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);
}
答案 0 :(得分:0)
问题在于您的SQL查询;
ResultSet rs1=select.executeQuery("select * from narocnik,prevoz");
如果要加入两个表,则应使用内连接,左连接,右连接或外连接等连接。但是当你在查询中使用逗号(,)连接两个表时,它将是外连接。它会给你多个结果;
用户内部加入喜欢 -
Select * from table1 inner join table2 on table1.id = table2.id;
希望它对你有用。