gpt4 book ai didi

java - 您好,需要有关 jTable 的帮助

转载 作者:行者123 更新时间:2023-12-01 15:54:23 26 4
gpt4 key购买 nike

我在这里遇到了从 ms access db 到 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)

感谢可以帮助我的人...:) :) :)

最佳答案

仅通过查看您的错误代码,您似乎没有足够的列。

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)

关于java - 您好,需要有关 jTable 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5373311/

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