2013-08-23 45 views
0

將新手分享到JTable。剛剛進入今天。 所以我能夠將我的數據從數據庫填充到JTable中。 我目前停留在編輯的單元格保存到數據庫中。將已編輯的單元格從JTable保存到數據庫

我讀過很多文章。但我還沒有找到一個

只是一個按鈕,然後保存!

這裏是我的代碼

public class DBEngine { 
Connection con = IQConnection.getConnectionMgrInstance().getConnection(); 
PreparedStatement pstm = null; 
ResultSet rs = null; 

public Vector getEmployee() throws Exception{ 
    Vector<Vector<String>> employeeVector = new Vector<Vector<String>>(); 

    try {   

     if (con != null) { 

      String query = "SELECT * FROM OGG.TBLSAMPLE"; 


      try { 
       pstm = con.prepareStatement(query); 
       rs = pstm.executeQuery(); 


       while(rs.next()) { 
        Vector<String> employee = new Vector<String>(); 
        employee.add(rs.getString(1)); //Empid 
        employee.add(rs.getString(2)); //name 
        employee.add(rs.getString(3)); //position 
        employee.add(rs.getString(4)); //department 
        employeeVector.add(employee); 
       } 



      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     }else{ 
      //lbl_err1.setText("Error"); 
     } 

    } catch (Exception e) { 

     e.printStackTrace(); 
     //flag = false; 
     //lbl_err1.setText("Failed!"); 
    } finally { 

     try{ 
      if(rs != null){ 
       rs.close(); 
      } 
     }catch(Exception e){ 

     } 

     try{ 
      if(pstm != null){ 
       pstm.close(); 
      } 
     }catch(Exception e){ 

     }  
    } 
    return employeeVector; 
} 
} 

謝謝!

+0

哪裏是你保存到數據庫的代碼? – SpringLearner

+0

目前沒有。仍在研究並希望找到一個例子。 – user2510841

+0

@javaBeginner你對我的代碼有問題嗎? – user2510841

回答

0

利用PreparedStatement像這樣用於插入DB

PreparedStatement pt=con.prepareStatement("insert into mytable values(?) "); 
pt.setString(1,ID); 
int result=pt.executeUpdate(); 

如果要更新,然後用這種方式

PreparedStatement pt=con.prepareStatement("update mytable set column_name=? "); 
    pt.setString(1,ID); 
    int result=pt.executeUpdate(); 
+0

或者如果您只想更新數據庫表中的現有行而不是插入新行,則可以使用SQL'update'語句。 –

+0

PreparedStatement ps = con.prepareStatement(「UPDATE SAMPLE SET CLIENT =?,PARTNER =?,price =?WHERE id =」+ row); ps.setString(1,(String)jTblSample.getValueAt(row,1)); ps.setInt(2,(Integer)jTblSample.getValueAt(row,2)); ps.setInt(3,(Integer)jTblSample.getValueAt(row,3)); ps.executeUpdate(); – user2510841

+0

pt.setString(1,ID); < - 這是動態的。如果我編輯第三行的列ID – user2510841

相關問題