表单1是文本字段,表单2是JTable如何在表单1文本字段中检索表的值

时间:2022-06-03 22:02:35

Form 1 is the textfield located

表格1是位于的文本字段

private void tblOrgMouseClicked(java.awt.event.MouseEvent evt) {                                    

    Connection cn = null;
    Statement st = null;
    ResultSet rss = null;
    btnSave.setEnabled(false);
    btnUpdate.setEnabled(true);
    btnDelete.setEnabled(true);

    try {
        int row = tblOrg.getSelectedRow();
        String cell_click = (tblOrg.getModel().getValueAt(row, 0).toString());
        String sql = "SELECT * FROM tbl_organization WHERE org_id = '" + cell_click + "'";
        cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_organization?zeroDateTimeBehavior=convertToNull", "root", "");
        st = cn.prepareStatement(sql);
        rss = st.executeQuery(sql);
        if (rss.next()) {
            String addid = rss.getString("org_id");
            txtOrgID.setText(addid);
            String addname = rss.getString("org_name");
            txtOrgName.setText(addname);
            String adddesc = rss.getString("org_description");
            txtOrgDesc.setText(adddesc);
            String addadviser = rss.getString("org_adviser");
            txtAdviserName.setText(addadviser);
        }
    } catch (Exception e) {

    }
}     

Form 2 is the Jtable

表格2是Jtable

private void tblAdviserList2MouseClicked(java.awt.event.MouseEvent evt) {                                             
    // TODO add your handling code here:
    Connection cn = null;
    Statement st = null;
    ResultSet rss = null;
    String ab = " ";
    try {
        int row = tblAdviserList2.getSelectedRow();
        String cell_click = (tblAdviserList2.getModel().getValueAt(row, 0).toString());
        String sql = "SELECT * FROM tbl_adviser WHERE adviser_id = '" + cell_click + "'";
        cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_organization?zeroDateTimeBehavior=convertToNull", "root", "");
        st = cn.prepareStatement(sql);
        rss = st.executeQuery(sql);
        if (rss.next()) {
            String addid = rss.getString("firstname").concat(ab).concat(rss.getString("middlename")).concat(ab).concat(rss.getString("lastname"));
            new FrmOrganization(addid);
            this.setVisible(false);
        }
    } catch (Exception e) {

    }
}    

1 个解决方案

#1


1  

To get the value from a particular cell:

要从特定单元格获取值:

Object cellValue = table.getValueAt(row, col);

Alternatively, you can create a TableModel where each row represents a person object, and add a method on that.

或者,您可以创建一个TableModel,其中每一行代表一个person对象,并在其上添加一个方法。

#1


1  

To get the value from a particular cell:

要从特定单元格获取值:

Object cellValue = table.getValueAt(row, col);

Alternatively, you can create a TableModel where each row represents a person object, and add a method on that.

或者,您可以创建一个TableModel,其中每一行代表一个person对象,并在其上添加一个方法。