简体   繁体   English

为什么JTable仅显示第一行数据?

[英]Why does JTable only present 1st row of data?

I'm starting to learn how to use databases and was trying to export the data from my h2 database into a JTable. 我开始学习如何使用数据库,并尝试将h2数据库中的数据导出到JTable中。 The table comes up with the correct number of rows, however, only the first row is filled with data. 该表提供了正确的行数,但是,只有第一行填充了数据。 The rest is a blank grid. 其余为空白网格。 I posted some code below for the JTable. 我在下面为JTable发布了一些代码。 If someone needs to see more code, I'll post it. 如果有人需要查看更多代码,我将其发布。

public class Table extends JTable{

    public static int rows;
    public static String[][] data;
    public static String[] columns = {"Author", "Customer", "Date"};

    public static void populateTable() throws ClassNotFoundException, SQLException{

//Server is name of the database class
        Server server = new Server();

        Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);        
        ResultSet rs = stat.executeQuery("SELECT * FROM data");
        rs.last();
        rows = rs.getRow();
        rs.beforeFirst();

        data = new String[3][rows];

        while(rs.next()){
           int i = 0;
           data[0][i] = rs.getString("Author");
           data[1][i] = rs.getString("Customer");
           data[2][i] = rs.getString("Date");
           System.out.println(rs.getString("Author"));
           i = i++;
        }
        rs.close();
    }
}

class MyTableModel extends DefaultTableModel{

        String[] columnNames = {"Author", "Customer", "Date"};

        MyTableModel() throws ClassNotFoundException, SQLException{
            addColumn(columnNames[0]);
            addColumn(columnNames[1]);
            addColumn(columnNames[2]);
        }

        @Override
        public int getRowCount() {
            return rows;
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public String getColumnName(int columnIndex) {
            return columnNames[columnIndex];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
                return data[columnIndex][rowIndex];

    }

I'm also able to print to the console all the data, but it just won't show up in the JTable. 我还可以将所有数据打印到控制台,但是不会显示在JTable中。 I have been stuck on this problem for hours but I don't know what I'm doing wrong. 我已经在这个问题上停留了几个小时,但是我不知道自己在做什么错。 Thanks in advance 提前致谢

This statement is a no-op since i is assigned before it is incremented 此语句是空操作,因为在递增之前已分配了i

i = i++;

just use 只是使用

i++;

Also initialize i before entering the loop 在进入循环之前还要初始化i

You should either use a for loop or declare i outside of your loop. 您应该使用for循环或在循环之外声明i。 As it stands, you are setting all data to row 0 (int i = 0); 就目前而言,您正在将所有数据设置为行0(int i = 0);

while(rs.next()){
           int i = 0; // this will run for every row
           data[0][i] = rs.getString("Author");
           data[1][i] = rs.getString("Customer");
           data[2][i] = rs.getString("Date");
           System.out.println(rs.getString("Author"));
           i = i++;
        }

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

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