i want to add all my data from DB to jtable, but using this code i get only the last row from DB, added to the table severelal times.
Can someone tell where is mistake?
public Vector getOrder() throws Exception
{
DbConnection();
Statement st = null;
ResultSet res =null;
String query="Select * From Orders;";
try{
st=connect.createStatement();
res=st.executeQuery(query);
Vector v =new Vector();
Vector<String> record = new Vector<String>();
int i=0;
while(res.next())
{
record.clear();
uzsakNr=res.getString("Uzsakymo_nr");
priemDat=res.getString("Priemimo_data");
irengPav=res.getString("Irenginio_pavadinimas");
model=res.getString("Modelis");
status=res.getString("Statusas");
grazDat=res.getString("Grazinimo_data");
clientId=Long.toString(res.getLong("ClientId"));
//record.addElement(i);
record.addElement(uzsakNr);
record.addElement(priemDat);
record.addElement(irengPav);
record.addElement(model);
record.addElement(status);
record.addElement(grazDat);
record.addElement(clientId);
//record.addElement("");
v.addElement(record);
i++;
}
//Zle vydaji, daji tyko trzy takiesame ostatnie
System.out.println(v);
return v;
}finally {
if(res!=null) res.close();
if(st!=null) st.close();
}
}
in another class
try{
Vector ve=db.getOrder();
String heading[]={"uzsakNr","priemDat","irengPav","model","status","grazDat","ClientID"};
Vector columnHeads= new Vector();
for (int i=0;i<heading.length; i++)
columnHeads.addElement(heading[i]);
table = new JTable(ve,columnHeads);
table.addMouseListener(new TableMouseListener());
scrollPane.setViewportView(table);
UzsakPanel.add(scrollPane);
scrollPane.setBounds(10, 10, 901, 200);
}catch(Exception e2){e2.printStackTrace();}
Firstly,
Replace the line containing
clear()withVector<String> record = new Vector<String>();Secondly, I think what you’re doing is probably correct but your printing is the problem.
Since you have a vector of vectors containing strings, I would print it like so