Elias
Elias

Reputation: 13

JTable rows count has default value (4 rows) ONLY

I have been developing small program in java, i made jtable and connect it to mysql, but it only shows 4 rows only no matter how big my data is...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});
            }
        }

Upvotes: 0

Views: 44

Answers (1)

Arnaud
Arnaud

Reputation: 17534

You are mixing columns and rows, for(i = 0; i <= columns; i++) will only read 4 records/rows (because you have 3 columns) .

Simply browse the resultset with while(rs.next()) to get all the data :

   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});

    }

Upvotes: 1

Related Questions