关于core java v2h6 中例子的问题 在构造器中原来的语句是;产生连接con产生陈述stmt,然后是从con中获得原数据,从原数据中产生ResultSet读表名,我改成rs = stmt.exe…… 这样会出错。后来改成,从con产生陈述stmtA,rs = stmtA.exe…… 这样就可以但是,程序启动时就有数据显示。(代码暂时无法附上)所谓正常运行是指没有错误产生而且可以看到每一个表里的数据 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 改成rs = stmt.exe…… 这样会出错这时你stmt是不是close? class ResultSetTableFrame extends JFrame{ private JScrollPane scrollPane; private ResultSetTableModel model; private JComboBox tableNames; private JButton nextButton; private JButton previousButton; private ResultSet rs; private Connection con; private Statement stmt; public ResultSetTableFrame() { this.setTitle("ResultSetTable"); this.setSize(300, 200); Container contentPane = this.getContentPane(); tableNames = new JComboBox(); tableNames.addActionListener(new TestListener()); JPanel panel = new JPanel(); panel.add(tableNames); contentPane.add(panel, BorderLayout.NORTH); try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:oci8:@mydb"; Connection conn = DriverManager.getConnection (url, "spcnet", "spcnet"); stmt = conn.createStatement(); /*DatabaseMetaData md = conn.getMetaData(); ResultSet rsa = md.getTables(null, null, null, new String[]{"TABLE"}); while (rsa.next()) tableNames.addItem(rsa.getString(3)); rsa.close();*/ Statement stmta = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rsa = stmta.executeQuery("select tname from tab where tabtype = 'TABLE'"); while (rsa.next()) tableNames.addItem(rsa.getString(1)); rsa.close(); } catch(Exception e) { System.out.println("Error1: " + e.toString()); } } private class TestListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (event.getSource() == tableNames) { if (scrollPane != null) getContentPane().remove(scrollPane); try { String tableName = (String)tableNames.getSelectedItem(); if (rs != null) rs.close(); String query = "select * from " + tableName ; rs = stmt.executeQuery(query); model = new ScrollingResultSetTableModel(rs); JTable table = new JTable(model); scrollPane = new JScrollPane(table); getContentPane().add(scrollPane, BorderLayout.CENTER); pack(); doLayout(); } catch(Exception e) { System.out.println("Error2 :" + e.toString()); } } } }}其中注釋掉的部分是原書中的,現在是不出錯誤的版本,其他部分沒有改變! 1."我的问题是:在读所有的表的时候,如果通过调用stmt.executeQuery方法,那么只能读到一个表的名字,而且在程序启动时面板上会显示该表的数据。";因为在tableNames.addItem(rsa.getString(1));的时候会触发TestListener这时执行rs = stmt.executeQuery(query);后 原来的rsa就被关闭了所以只能读到一个表的名字!“如果另外生成一Statement对象stmtA然后调用stmtA.executeQuery的话程序正常运行,但是在程序启动时面板上会显示数据,也就是说激发了监听事件。”同理在tableNames.addItem(rsa.getString(1));的时候会触发TestListener! 谁能提供点好的学习java的方法和书 不理解“汉诺塔”代码,大家能不能帮帮我 what 数据抽象 and 过程抽象 Statement究竟是干什么的啊,我这两个超简单的语句有错吗? 怎样用java构造post数据包发送到服务端 初学java用什么书好? 又有问题了! 在GB2312编码下,如何使输出(char)255能正常 怎么能向数据库中插入当前时间到日期类型的字段 收邮件(图像)-->显示邮件(图像)问题 这个用户自定义包程序那里有错 请问null!=myobj和myobj!=null有什么不同?
这时你stmt是不是close?
{
private JScrollPane scrollPane;
private ResultSetTableModel model;
private JComboBox tableNames;
private JButton nextButton;
private JButton previousButton;
private ResultSet rs;
private Connection con;
private Statement stmt; public ResultSetTableFrame()
{
this.setTitle("ResultSetTable");
this.setSize(300, 200); Container contentPane = this.getContentPane();
tableNames = new JComboBox();
tableNames.addActionListener(new TestListener());
JPanel panel = new JPanel();
panel.add(tableNames);
contentPane.add(panel, BorderLayout.NORTH); try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:oci8:@mydb";
Connection conn = DriverManager.getConnection (url, "spcnet", "spcnet");
stmt = conn.createStatement(); /*DatabaseMetaData md = conn.getMetaData();
ResultSet rsa = md.getTables(null, null, null, new String[]{"TABLE"});
while (rsa.next())
tableNames.addItem(rsa.getString(3));
rsa.close();*/
Statement stmta = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rsa = stmta.executeQuery("select tname from tab where tabtype = 'TABLE'");
while (rsa.next())
tableNames.addItem(rsa.getString(1));
rsa.close();
}
catch(Exception e)
{
System.out.println("Error1: " + e.toString());
}
} private class TestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == tableNames)
{
if (scrollPane != null)
getContentPane().remove(scrollPane);
try
{
String tableName = (String)tableNames.getSelectedItem();
if (rs != null)
rs.close();
String query = "select * from " + tableName ;
rs = stmt.executeQuery(query);
model = new ScrollingResultSetTableModel(rs); JTable table = new JTable(model);
scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane, BorderLayout.CENTER);
pack();
doLayout();
}
catch(Exception e)
{
System.out.println("Error2 :" + e.toString());
}
}
}
}
}其中注釋掉的部分是原書中的,現在是不出錯誤的版本,其他部分沒有改變!
因为在tableNames.addItem(rsa.getString(1));的时候会触发TestListener
这时执行rs = stmt.executeQuery(query);后 原来的rsa就被关闭了所以只能读到一个表的名字!“如果另外生成一Statement对象stmtA然后调用stmtA.executeQuery的话程序正常运行,但是在程序启动时面板上会显示数据,也就是说激发了监听事件。”
同理在tableNames.addItem(rsa.getString(1));的时候会触发TestListener!