简体   繁体   English

我想显示从数据库到jTable的值

[英]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.

相关问题 我想在mysql的jtable中显示记录 - i want to display records in jtable from mysql 无法在JTable中显示行(数据库中的值) - Cant display rows in a JTable (Values from Database) 如何在Java中从数据库向jtable显示信息? - How Do I display info from my database to a jtable in java? JTable 错误:我试图在文本字段中显示来自 JTable 的选定行值,但我不断收到此错误: - JTable error: I'm trying to display the selected row values from a JTable in the textfield but I keep getting this error: 需要一些建议:我想使用jTable在ArrayList中添加和显示对象,然后将这些记录添加到SQL数据库中 - Going to need some advice: I want to add and display objects in an ArrayList using jTable, then add these records to SQL database 我想从数据库中检索值而不使用listview并在textview领域中显示 - I want to retrieve values from database without using listview and display in textview feilds 如何显示从数据库到jtable的所有数据? - how to display all the data from the database to jtable? 我想在JButton中动态显示数据库值作为测试 - I want to Dynamic display Database values as test in JButton 如何从数据库填充jtable复选框值? - How to populate jtable checkbox values from database? 从数据库表在JTable中添加值 - Add values in JTable from Database Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM