JTable仅显示来自访问数据库的一个列

时间:2016-10-16 16:47:18

标签: java swing ms-access jtable

我试图显示来自访问数据库的数据;出于某种原因,每当我运行程序时,只显示一行。这是我的代码

try{
    String coursec = jTextField9.getText().trim();
    String sql5 = "SELECT * FROM Studentcourse WHERE ccode ='" +coursec+"'";
    String url5 ="jdbc:ucanaccess://C:/Users/james_000/Documents/NetBeansProjects/Registration/Campus.accdb ";
    Connection conn5 =DriverManager.getConnection(url5);
    Statement statem = conn5.createStatement();
    ResultSet rs5 =  statem.executeQuery(sql5);
    while (rs5.next()){
        DefaultTableModel dm;
        dm = new DefaultTableModel(10, 10);
        String coursecode= rs5.getString(2);
        String attend =rs5.getString(3);
        String date =rs5.getString(4);
        Vector <String> vector = new Vector<String>();
        vector.add(coursecode);
        vector.add(attend);
        vector.add(date);
        String s[] = new String[]{"ccode", "stnumattend", "Date"};
        dm.setColumnIdentifiers(s);
        jTable1.setModel(dm);
        dm.addRow(vector);
        jTable1.setVisible(true);
        }
    }
    catch(Exception d){
        System.err.println("Exception:" + d.getMessage());  
    }

1 个答案:

答案 0 :(得分:1)

您每次通过DefaultTableModel循环创建 while,因此只有 last 这样的模型适用于jTable1。至少,将模型创建移出循环。

DefaultTableModel dm = new DefaultTableModel(10, 10);
jTable1.setModel(dm);
while (rs5.next()) {
    String coursecode= rs5.getString(2);
    …
}