简体   繁体   English

如何在一个JTable中连接来自2个不同表的数据

[英]How to Join data from 2 different tables in a single JTable

Im making a program for employee management system.. Now I am making the CRUD form for Employee registration, i seperated the src code in data layer , business logic and gui.. In gui I separated gui view and gui model, in Gui model i created a EmployeeTableModel , to specify how I want to show data for employee Table , but in that table i got datas that come from more than one table , one is Employee table from database another one is Phone .. I have successfully get the data from Employee Table , and i can put those data in both Employee Table and Phone table in database , but I can't get them into the TableModel, I can only get the datas from employee. 我正在为员工管理系统制作一个程序。现在我正在为员工注册制作CRUD表单,我在数据层,业务逻辑和gui中分离了src代码。在gui中我分离了gui视图和gui模型,在Gui模型中我创建了一个EmployeeTableModel,用于指定我想如何显示employee表的数据,但在该表中我得到的数据来自多个表,一个是来自数据库的Employee表,另一个是Phone ..我已成功获取数据Employee Table,我可以将这些数据放在数据库的Employee Table和Phone表中,但是我无法将它们放入TableModel,我只能从员工那里获取数据。

PHOTO OF THE TABLE 表的照片

ALL SOURCE CODE HERE - GIT 所有来源代码在这里 - GIT

I tried changing the code in my EmployeeTableModel to accept more columns and to make a way to add datas from telephone table too , but didn't worked because each employee has 2 -3 phone numbers and the Employee_ID is specified as a foreign key on Phone table ..Each employee has 3 phones as I said and when we specify which phone we want to get , it's depenedent from employee_id that is foreign key in Phone table. 我尝试更改EmployeeTableModel中的代码以接受更多列并从电话表中添​​加数据,但是没有用,因为每个员工都有2-3个电话号码,而Employee_ID被指定为电话上的外键table ..每个员工都有3部电话,正如我所说,当我们指定要获得哪部电话时,它取决于employee_id,即电话表中的外键。

public class PunetoriTableModel extends AbstractTableModel {

List<Punetori> list;
TelefoniRepository tr = new TelefoniRepository();
PunetoriRepository pr = new PunetoriRepository();

String[] cols = {"Nr.", "Nr-Departmentit", "Emri", "Mbiemri", "Email", "Qyteti","Adresa","tel1","tel2"};

public PunetoriTableModel() {
}

public PunetoriTableModel(List<Punetori> list) {
    this.list = list;
}

public void addList(List<Punetori> list) {
    this.list = list;
}

@Override
public String getColumnName(int col) {
    return cols[col];
}

@Override
public int getRowCount() {
    return list.size();
}

public void remove(int row) {
    list.remove(row);
}

public Punetori getPersoni(int index) {
    return list.get(index);
}

@Override
public int getColumnCount() {
    return cols.length;
}

public String getDateToString(Date d) {
    DateFormat da = new SimpleDateFormat("dd-MM-yyyy");
    return da.format(d);

}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
     Punetori p = list.get(rowIndex) ;


        switch (columnIndex) {
            case 0:
                return p.getPunetoriID();
            case 1:
                return p.getDepartmentiId();
            case 2:
                return p.getEmri();
            case 3:
                return p.getMbiemri();
            case 4:
                return p.getEmail();
            case 5:
                return p.getQyteti();
            case 6:
                return p.getAdresa();




            default:
                return null;
        }
    }
}

First of all decide what your columns are going to be. 首先确定您的列将是什么。 You seem to be confused what these columns should be, when some employees have more than one phone. 当一些员工拥有多部手机时,您似乎对这些列应该是什么感到困惑。 It is not clear what your problem is, but probably just drawing it on a piece of paper will already clear your mind. 目前尚不清楚你的问题是什么,但可能只是把它放在一张纸上就已经清楚了。

Decide also whether these columns are going to be fixed, or dynamic (ie is it possible to get more columns for some employees, that you will need to add to your table?) 还要确定这些列是固定的还是动态的(即是否可以为某些员工获取更多列,您需要添加到表中?)

Second, create a separate data structure, lets call this class EmployeeData , that captures the information you want. 其次,创建一个单独的数据结构,让我们调用这个类EmployeeData ,它捕获你想要的信息。 Make this separate from the PunetoriTableModel . 将它与PunetoriTableModel分开。 Populate this from your data repositories. 从您的数据存储库中填充此内容。 Inside this put the necessary logic that decides which phone to use, or whatever custom business logic you need. 在此内部提出了必要的逻辑,决定使用哪种手机,或者您需要的任何自定义业务逻辑。

Make the EmployeeData provide a few public methods like getColumns() , getRowCount() and getRow(int i) etc. 使EmployeeData提供一些公共方法,如getColumns()getRowCount()getRow(int i)等。

In the constructor of your PunetoriTableModel just pass the EmployeeData and inside the respective methods call the EmployeeData ones. PunetoriTableModel的构造函数中,只需传递EmployeeData并在各自的方法中调用EmployeeData This way the table model sees the data as a table, and you have your custom logic which combines it from different tables into one which is separate. 通过这种方式,表模型将数据视为一个表,并且您拥有自定义逻辑,该逻辑将不同表中的数据组合成一个独立的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM