2012-09-06 29 views
-2

我在netbeans中使用jTable。選擇組合框後,如果我選擇了員工ID 1,它將在jTable中顯示emp 1的所有數據。但是,當我選擇emmployee ID 2 JTable的下一次顯示EMP id爲1的單值及以下如何在jTable中顯示數據在JAVA中附加舊值

Connect c  = new Connect();//connection to database 
con    = (Connection) c.getConnection(); 
st    = (Statement)con.createStatement(); 
String ddate = (String)text.getSelectedItem(); 
System.out.println("id " +ddate); 
rs = st.executeQuery("select e.employee_id,e.`first_name`, i.outtime, i.outtime_date from tbl_employee e,tbl_outtime i where e.employee_id=i.outtime_emp_id and i.`outtime_date` LIKE '%/"+month2+"/"+year1+"'and outtime_emp_id="+ddate); 
while(rs.next()) 
{ 
    String dat1=rs.getString("outtime_date"); 
    String e1=rs.getString("employee_id"); 
    System.out.println(e1); 
    st33=(Statement) con.createStatement();  
    rs33=st33.executeQuery("select i.intime, i.intime_date from tbl_employee e,tbl_intime 
    i where e.employee_id=i.intime_emp_id and i.`intime_date`='"+dat1+"' and 
    i.intime_emp_id="+e1); 
    if(rs33.next()) 
    { 
     int emp=rs.getInt("employee_id"); 
     System.out.println(emp); 
     String name=rs.getString("first_name"); 
     String dept=rs33.getString("intime"); 
     String desig=rs.getString("outtime"); 
     String join=rs33.getString("intime_date"); 
     jTable1.setValueAt(emp, cnt, 0); 
     jTable1.setValueAt(name, cnt, 1); 
     jTable1.setValueAt(dept, cnt, 2); 
     jTable1.setValueAt(desig, cnt, 3); 
     jTable1.setValueAt(join, cnt, 4); 
     cnt=cnt+1; 
    } 
} 

我的代碼被賦予EMPID 2. 所有其他值告訴我解決辦法,如果有人知道。

+2

很難說任何切肉刀,爲更好的幫助儘早發佈[SSCCE](http://sscce.org/) – mKorbel

+0

oky,但你能告訴我,我可以清除jTable,而點擊組合框 – Amruta

+0

table.setModel(new YourTableModel ()); – MadProgrammer

回答

1

與所有其他Swing組件一樣,表由兩部分組成:視圖部分(JTable)和模型部分(TableModel)。當模型通過投擲事件表明它已被更改時,視圖會更新。有關更多信息,請參閱table tutorial

因此,對於您的用例,您可以調整現有的TableModel或創建一個新的。由於您正在使用數據庫,因此我會親自選擇創建一個新的。

請注意,Swing組件只能在事件派發線程上訪問和修改,並且長時間運行的任務(查詢數據庫)不應該在此線程上發生(請參閱Swing concurrency tutorial)。這就是爲什麼我會建議創建一個新的TableModel。您可以在用於查詢數據庫的工作線程上創建此模型,並且可以在Event Dispatch Thread中一次性替換模型。 SwingWorker班是最適合這個。