我在這裏顯示從ms access db到jtable的日期類型的問題...我似乎無法得到它的工作,並嘗試修改我的代碼,但仍然沒有幸運我希望有人在這裏可以提供我的另一個想法...你好,在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)
感謝有人可以幫助我... :) :) :)
你會得到任何錯誤?如果是的話,在這裏發帖.. – 2011-03-21 02:44:09
我不確定,它的代碼行號是1302嗎?因爲它在「UsernamePasswordPackage.MainForm.PopulatejTable(MainForm.java:1302)」中擊中了異常「...檢查出它。 – 2011-03-21 02:51:15
我收到一個新的錯誤,說[Microsoft] [ODBC Microsoft Access Drivers]無效的描述符索引。謝謝你們,你們對我的項目幫了我很多......很多很多謝謝:) :) :) – 2011-03-21 03:07:13