gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:05:01 25 4
gpt4 key购买 nike

我一直在用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});
}
}

最佳答案

您正在混合列和行,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});

}

关于java - JTable 行数仅具有默认值(4 行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41552757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com