JTable行计数仅具有默认值(4行)

时间:2017-01-09 16:42:43

标签: java mysql jtable

我一直在开发java中的小程序,我制作jtable并将它连接到mysql,但无论我的数据有多大,它都只显示4行... here is jtable in java

and here is mysql table data..

con = DBconnect.connect();
    String[] columnNames = {"Name", "UserName", "UserType"};
    model.setColumnIdentifiers(columnNames);

    jTable1.setModel(model);

    jTable1.setShowGrid(false);
    try {
        PreparedStatement pst;
        pst = con.prepareStatement("select * from courses");
        ResultSet rs = pst.executeQuery();
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();
        int i = 0;
        for(i = 0; i <= columns; i++){
            if (rs.next()) {
                uname = rs.getString("C_ID");
                email = rs.getString("C_NAME");
                pass = rs.getString("C_TYPE");
                model.addRow(new Object[]{uname, email, pass});
            }
        }

1 个答案:

答案 0 :(得分:1)

您正在混合列和行,for(i = 0; i <= columns; i++)只会读取4条记录/行(因为您有3列)。

只需使用while(rs.next())浏览结果集即可获取所有数据:

   while(rs.next()){

            uname = rs.getString("C_ID");
            email = rs.getString("C_NAME");
            pass = rs.getString("C_TYPE");
            model.addRow(new Object[]{uname, email, pass});

    }