[英]I want to display values from Database to jTable
I have a Java program called employeeRecords
, which I use to do CRUD operations in a SQL Server database. 我有一个名为
employeeRecords
的Java程序,用于在SQL Server数据库中执行CRUD操作。
I have another program called Table
which is a jTable program. 我有另一个名为
Table
程序,它是一个jTable程序。 I need to display employeeRecords
values into my jTable. 我需要在我的jTable中显示
employeeRecords
值。 I know how to display a record in textField but not in jTable columns. 我知道如何在textField中显示记录,但不在jTable列中显示。
If it matters I'm using Eclipse Mars. 如果有问题,我正在使用Eclipse Mars。
Start by having a look at How to Use Tables 首先看一下如何使用表格
The answer is quite complex, depending on what you want to achieve. 答案非常复杂,具体取决于您要实现的目标。
I'd suggest starting by creating a simple POJO to hold the data from the database, personally, this makes the management much easier... 我建议首先创建一个简单的POJO来保存数据库中的数据,就个人而言,这使管理更加容易...
public class Employee {
// Fields, getters and setters
}
Next, you'll want a TableModel
to manage all the instances of Employee
... 接下来,您将需要一个
TableModel
来管理Employee
所有实例。
public class EmployeeTableModel extends AbstractTableModel {
private List<Employee> employees;
public EmployeeTableModel(List<Employee> employees) {
this.employees = new ArrayList<>(employees);
}
@Override
public int getRowCount() {
return employees.size();
}
@Override
public int getColumnCount() {
return how ever many fields you want to display;
}
@Override
public String getColumnName(int column) {
String name = "??";
switch (column) {
case 0:
name = "What every this column represents"
break;
.
.
.
}
return name;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Employee employee = employees.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = employee.getXxx();
break;
.
.
.
}
return value;
}
}
Personally, I find it easier to have a custom table for each POJO, as it allows you manage the individual rows/objects more easily, but that's me 就个人而言,我发现为每个POJO都有一个自定义表比较容易,因为它使您可以更轻松地管理各个行/对象,但这就是我
Next, you need to load the data from the database and populate the table model... 接下来,您需要从数据库加载数据并填充表模型。
public EmployeeTableModel getEmployeeTableModel() throws SQLException {
Connection con = ...;
EmployeeTableModel model = null;
try (PreparedStatement stmt = con.prepareStatement("select * from employeRecords")) {
try (ResultSet rs = stmt.executeQuery()) {
List<Employee> employees = new ArrayList<>(25);
while (rs.next()) {
Employee employee = new Employee();
employee.setXxx(rs.getString("Xxx"));
//...
}
model = new EmployeeTableModel(employees);
}
}
return model;
}
Finally, you need to apply the model to a instance of JTableModel
最后,您需要将模型应用于
JTableModel
的实例
public class SomeUIClass extends JPanel {
//...
private JTable table;
//...
public void someMethodYouNeedToCallWhenYouWantToReloadTheModel() {
try {
table.setModel(getEmployeeTableModel());
} catch (SQLException ex) {
ex.printStackTrace();
}
}
I don't know if you know how to access the database etc but to print the data to the JTable use this constructor. 我不知道您是否知道如何访问数据库等,但是使用此构造函数将数据打印到JTable。
JTable(Vector rowData, Vector columnNames) Constructs a JTable to display the values in the Vector of Vectors, rowData, with column names, columnNames.
Just remember that the rowData parameter is a Vector of Vectors, with position 0 being the first row under the headings etc. 只需记住rowData参数是向量的Vector,位置0是标题等下的第一行。
If this doesn't answet your question please be a little bit more specific and I will see what I can help you with 如果这不能解决您的问题,请更具体一些,我将为您提供帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.