数据库连接没有异常,代码能跑
package cn.abtc.ManagementSystemUI; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; public class TableTestDemo extends JFrame implements ActionListener { // 定义组件 private JScrollPane scrollBar; private JTableHeader header; private JTable StudentInfo; private JButton allStudentsInfo; private JFrame ModificationOfStudentStatusFrame; // 构造方法 public TableTestDemo() { // 窗体的相关属性的定义 ModificationOfStudentStatusFrame = new JFrame(); // 创建组件 scrollBar = new JScrollPane(); allStudentsInfo = new JButton("显示当前全部学籍"); scrollBar.setBounds(10, 50, 600, 300); allStudentsInfo.setBounds(10, 10, 600, 30); // 注册监听 allStudentsInfo.addActionListener(this); //设置窗体属性 ModificationOfStudentStatusFrame.setSize(635, 400); ModificationOfStudentStatusFrame.setLayout(null); //ModificationOfStudentStatusFrame.setLayout(new GridLayout(2,1)); ModificationOfStudentStatusFrame.setLocation(100, 50); ModificationOfStudentStatusFrame.add(allStudentsInfo); ModificationOfStudentStatusFrame.add(scrollBar); ModificationOfStudentStatusFrame.setVisible(true); } // 点击按钮时的事件处理 public void actionPerformed(ActionEvent arg0) { try { Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/UserInfo", "admin", "admin"); // 建立查询条件 String sql = "select * from EnrollmentStatus"; Statement stmt = connect.createStatement(); // 执行查询 ResultSet rs = stmt.executeQuery(sql); // 计算有多少条记录 int count = 0; while (rs.next()) { count++; } // rs = stmt.executeQuery(); // 将查询获得的记录数据,转换成适合生成JTable的数据形式 Object[][] info = new Object[count][9]; count = 0; while (rs.next()) { info[count][0] = rs.getString("studentID"); info[count][1] = rs.getString("name"); info[count][2] = rs.getString("sex"); info[count][3] = rs.getString("nation"); info[count][4] = rs.getString("dateOfBirth"); info[count][5] = rs.getString("address"); info[count][6] = rs.getString("phoneNumber"); info[count][7] = rs.getString("timeOfEnrollment"); info[count][8] = rs.getString("IDNumber"); count++; } // 定义表头 String[] title = { "学 号:", "姓 名:", "性 别:", "民 族:", "出生日期:", "家庭住址:", "电 话:", "入学时间:", "身份证号:" }; // 创建JTable StudentInfo = new JTable(info, title); // 显示表头 //header = StudentInfo.getTableHeader(); // 将JTable加入到带滚动条的面板中 scrollBar.getViewport().add(StudentInfo); } catch (Exception e) { JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE); } } public static void main(String[] args) { new TableTestDemo(); } }
有谁能告诉本人一下吗?谢谢了!
解决方案
30
建议单步调试一下程序,看看红色部分代码有没有被执行。 显然不会被执行。
while (rs.next()) {
count++;
}
// rs = stmt.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][9];
count = 0;
while (rs.next()) {
info[count][0] = rs.getString(“studentID”);
info[count][1] = rs.getString(“name”);
info[count][2] = rs.getString(“sex”);
info[count][3] = rs.getString(“nation”);
info[count][4] = rs.getString(“dateOfBirth”);
info[count][5] = rs.getString(“address”);
info[count][6] = rs.getString(“phoneNumber”);
info[count][7] = rs.getString(“timeOfEnrollment”);
info[count][8] = rs.getString(“IDNumber”);
count++;
}
while (rs.next()) {
count++;
}
// rs = stmt.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][9];
count = 0;
while (rs.next()) {
info[count][0] = rs.getString(“studentID”);
info[count][1] = rs.getString(“name”);
info[count][2] = rs.getString(“sex”);
info[count][3] = rs.getString(“nation”);
info[count][4] = rs.getString(“dateOfBirth”);
info[count][5] = rs.getString(“address”);
info[count][6] = rs.getString(“phoneNumber”);
info[count][7] = rs.getString(“timeOfEnrollment”);
info[count][8] = rs.getString(“IDNumber”);
count++;
}
10
不是gc回收了,而是rs.next已经把全部结果遍历过,游标指向最后一个结果,所以你第二次rs.next的时候是空的。你应该只用一次rs.next。
另外为什么要写第一个rs.next呢,你下面的遍历不是同样有count++;不是也能统计结果数量吗。
另外为什么要写第一个rs.next呢,你下面的遍历不是同样有count++;不是也能统计结果数量吗。