0

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;
}
}

谢谢!

4

1 回答 1

0

像这样使用preparedstatement插入数据库

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();
于 2013-08-23T09:05:03.977 回答