'How to display data from 2 tables in Netbeans from ORACLE database
I don't know how to properly display data from 2 tables in the TextArea field. I added 2 rows in the database tables but the program displays 4 rows in the textArea instead of 2(duplicate), if I add 3 rows program displays 3xfirst row,3x second row,3x third row...etc.This is my code. Please help me in some way :(
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);
}
Solution 1:[1]
Problem is in your SQL Query;
ResultSet rs1=select.executeQuery("select * from narocnik,prevoz");
If you want to join two tables you should use joining like inner join, left join, right join or outer join. But when you just use comma (,) in your query to join two tables it will be outer join. It will give you multiple result;
User Inner Join like -
Select * from table1 inner join table2 on table1.id = table2.id;
Hope it will work for you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Iqbal Hossain |
