gpt4 book ai didi

java - ResultSet 的结果未出现在 jtable 中

转载 作者:行者123 更新时间:2023-12-02 06:29:28 26 4
gpt4 key购买 nike

尽管它显示 ResultSet 中有结果(行),但将这些行放入 JTable 时会出现问题。当我运行该程序时,从 MS Access 数据库检索的结果没有出现在 JTable 上。请帮忙 !我的代码如下!

final JTable tb1 = new JTable();

retcus.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame3.setVisible(true);
frame1.setEnabled(true);
frame.setVisible(false);
frame2.setVisible(false);

try{

Connection con;
Statement stmt;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String cn = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=E:/userlogin.mdb";
con = DriverManager.getConnection(cn,"","");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql = "select * from userlogin.customers";
ResultSet rs = stmt.executeQuery(sql);


Vector rows=new Vector();

while(rs.next()){

Vector one_row=new Vector();

one_row.add(rs.getString("ID"));
one_row.add(rs.getString("fname"));
one_row.add(rs.getString("sname"));
one_row.add(rs.getString("address"));
one_row.add(rs.getString("city"));
one_row.add(rs.getString("contact"));

rows.add(one_row);

}

DefaultTableModel model=new DefaultTableModel();

Iterator i=rows.iterator();
int count=0;
while(i.hasNext()){
model.insertRow(count,(Vector)i.next());
count++;
}

tb1.setModel(model);

int size = 0;
try {
rs.last();
size = rs.getRow();
rs.beforeFirst();
}

catch(Exception ex) {

}
JOptionPane.showOptionDialog(null,
size,
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");

if (rs==null)
{
JOptionPane.showOptionDialog(null,
"Resultset null !",
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");
}
rs.close();
stmt.close();


}

catch (HeadlessException err) {
JOptionPane.showOptionDialog(null,
"HeadlessException !",
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");
}

catch (ClassNotFoundException err) {
JOptionPane.showOptionDialog(null,
"ClassNotFoundException !",
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");

}
catch (SQLException err) {
JOptionPane.showOptionDialog(null,
err.getMessage(),
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");

}

}
});



panel3.add(tb1);
size = new Dimension(600, 150);
tb1.setBounds(100 + insets3.left, 140 + insets3.top, size.width, size.height);

最佳答案

panel3.add(tb1);
size = new Dimension(600, 150);
tb1.setBounds(100 + insets3.left, 140 + insets3.top, size.width, size.height);

a) 不要使用 setBounds()。 Swing 被设计为与布局管理器一起使用

b) 应将表格添加到 JScrollPane,然后将滚动 Pane 添加到面板

c) 当您将组件添加到可见框架时,基本代码是:

 panel.add(scrollPane);
panel.revalidate();
panel.repaint();

关于java - ResultSet 的结果未出现在 jtable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20208254/

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