您好,需要有关jTable的帮助

时间:2011-03-21 02:40:37

标签: java netbeans jtable

我有一个问题,显示从ms访问数据库到jtable的日期类型...我似乎无法让它工作,并尝试修改我的代码,但仍然没有运气,我希望有人在这里可以给我另一个想法...

这是我的示例代码,用数据库中的数据填充我的jtable:

public void PopulatejTable(){
    DefaultTableModel model =(DefaultTableModel)jTable1.getModel();

    jTable1.getColumnModel().getColumn(0).setPreferredWidth(35);
    jTable1.getColumnModel().getColumn(1).setPreferredWidth(200);
    jTable1.getColumnModel().getColumn(2).setPreferredWidth(100);
    jTable1.getColumnModel().getColumn(3).setPreferredWidth(150);
    jTable1.getColumnModel().getColumn(4).setPreferredWidth(100);

    String dbuser = "";
    String dbpasswd = "";
    String DriverPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\myWork2\\PersonalInfo\\db1.mdb";

    String SQLString = ("select ID,Username,Password,Website from Table1 order by ID");

    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }catch(Exception e){
        JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC DRIVER Error",JOptionPane.WARNING_MESSAGE);
        return;
    }
    PreparedStatement stmt = null;
    Connection con = null;
    ResultSet rset = null;

    try{
        con = DriverManager.getConnection(DriverPrefix,dbuser,dbpasswd);
        stmt = con.prepareStatement(SQLString);
    }catch(Exception e){
        JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection Error",JOptionPane.WARNING_MESSAGE);
    }
    while(model.getRowCount()>0){
        model.removeRow(0);
    }

    try{
        rset = stmt.executeQuery();
        while(rset.next()){
            int ID = rset.getInt(1);
            String Username = rset.getString(2);
            String Password = rset.getString(3);
            String Website = rset.getString(4);
            Date DateEncoded = rset.getDate(5);
            model.addRow(new Object[]{ID,Username,Password,Website,DateEncoded});
        }
        rset.close();
        stmt.close();
        con.close();
    }catch(SQLException e){
           JOptionPane.showMessageDialog(null,e.getMessage(),"Communication Error",JOptionPane.WARNING_MESSAGE);
    }
}

这是我得到的错误:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
    at UsernamePasswordPackage.MainForm.PopulatejTable(MainForm.java:1302)
    at UsernamePasswordPackage.MainForm.<init>(MainForm.java:71)
    at UsernamePasswordPackage.MainForm$29.run(MainForm.java:1398)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

感谢能帮助我的人...... :) :) :)

1 个答案:

答案 0 :(得分:1)

只需查看您的错误代码,您似乎没有足够的列。

jTable1.getColumnModel().getColumn(4).setPreferredWidth(100);


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4 at java.util.Vector.elementAt(Vector.java:427) at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)