| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4202 人关注过本帖
标题:求教一下java.sql.SQLException: ResultSet is closed问题该如何解决
只看楼主 加入收藏
antonio_jiao
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-6
收藏
 问题点数:0 回复次数:15 
求教一下java.sql.SQLException: ResultSet is closed问题该如何解决

请教一下,最近在做的一道题目,功能基本实现了,但是结束后,跳出异常
java.sql.SQLException: ResultSet is closed

请问该怎样才能解决啊,先谢谢拉

代码如下
package exam2;
import java.sql.*;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class BuyFrame extends JFrame {
JPanel contentPane;
JLabel lblTitle = new JLabel();
JLabel lblKind = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel lblNum = new JLabel();
JTextField txtNum = new JTextField();
JTextField txtNo = new JTextField();
JPanel jPanel1 = new JPanel();
JRadioButton radSeat = new JRadioButton();
JRadioButton radBed = new JRadioButton();
JButton btnOk = new JButton();
JOptionPane jOptionPane1 = new JOptionPane();
public BuyFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(300, 400));
setTitle("火车票订购");
lblTitle.setFont(new java.awt.Font("Dialog", Font.PLAIN, 16));
lblTitle.setText("订购火车票");
lblTitle.setBounds(new Rectangle(104, 48, 82, 32));
lblKind.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
lblKind.setText("乘车方式 :");
lblKind.setBounds(new Rectangle(42, 152, 100, 30));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
jLabel2.setText("火车车次:");
jLabel2.setBounds(new Rectangle(42, 107, 100, 30));
lblNum.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
lblNum.setText("订购数量:");
lblNum.setBounds(new Rectangle(42, 197, 100, 30));
txtNum.setText("");
txtNum.setBounds(new Rectangle(137, 200, 116, 26));
txtNo.setText("");
txtNo.setBounds(new Rectangle(137, 107, 116, 26));
jPanel1.setBounds(new Rectangle(128, 153, 138, 33));
radSeat.setToolTipText("");
radSeat.setText("硬座");
radBed.setText("软卧");
btnOk.setBounds(new Rectangle(100, 275, 100, 34));
btnOk.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
btnOk.setText(" 确定");
btnOk.addActionListener(new BuyFrame_btnOk_actionAdapter(this));
jOptionPane1.setBounds(new Rectangle(18, 134, 269, 199));
jOptionPane1.setVisible(false);
contentPane.add(lblTitle);
contentPane.add(btnOk);
contentPane.add(txtNo);
contentPane.add(txtNum);
contentPane.add(lblNum);
contentPane.add(lblKind);
contentPane.add(jPanel1);
jPanel1.add(radSeat);
jPanel1.add(radBed);
contentPane.add(jLabel2);
contentPane.add(jOptionPane1);
}

public void btnOk_actionPerformed(ActionEvent e) {
Connection con;
Statement sm;
ResultSet rs;

Conn conn = null;

if (conn == null) {
conn = new Conn();
}


try {
con = conn.getCon();
sm = con.createStatement();
String No=txtNo.getText();
int Num=Integer.parseInt(txtNum.getText());
rs=sm.executeQuery("select * from TrainTicket");

while(rs.next()){
String no1=rs.getString("TrainNo");
if (No.equals(no1.trim())){
int sNum=rs.getInt("seatNum");
int bNum=rs.getInt("bedNum");
float sPrice=rs.getFloat("seatPrice");
float bPrice=rs.getFloat("bedPrice");
if ((Num<=sNum) & this.radSeat.isSelected()){
int result=sNum-Num;
sm.execute("update TrainTicket set seatNum="+result+" where TrainNo='"+No+"'");
float STotalPrice=sPrice*Num;
JOptionPane.showConfirmDialog(null,"车票订购成功,请支付"+STotalPrice+"元","alert",JOptionPane.DEFAULT_OPTION,JOptionPane.OK_CANCEL_OPTION);

}


else if ((Num<=bNum) & this.radBed.isSelected()){
int result=bNum-Num;
sm.execute("update TrainTicket set bedNum="+result+" where TrainNo='"+No+"'");


float BTotalPrice=bPrice*Num;
JOptionPane.showConfirmDialog(null,"车票订购成功,请支付"+BTotalPrice+"元","alert",JOptionPane.DEFAULT_OPTION,JOptionPane.OK_CANCEL_OPTION);

}

else{
JOptionPane.showConfirmDialog(null,"对不起,票剩余不足","alert",JOptionPane.DEFAULT_OPTION,JOptionPane.OK_CANCEL_OPTION);
}

}
}
rs.close();
sm.close();
con.close();
} catch (Exception ee) {
ee.printStackTrace();
}

}
}


class BuyFrame_btnOk_actionAdapter implements ActionListener {
private BuyFrame adaptee;
BuyFrame_btnOk_actionAdapter(BuyFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.btnOk_actionPerformed(e);
}
}

搜索更多相关主题的帖子: java ResultSet SQLException closed sql 
2007-07-25 14:10
antonio_jiao
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-6
收藏
得分:0 
前面有朋友发过这道题目的例题,但是好像做的比较高深,毕竟,只有自己写,才能真正的学到东西,敬请不吝赐教,帮我看看引起这个异常的解决方法,先谢过了
2007-07-25 14:13
jerry_shen
Rank: 1
等 级:新手上路
帖 子:77
专家分:8
注 册:2007-7-25
收藏
得分:0 
把rs.close();
sm.close();
con.close();
统统放到finally块里去。

2007-07-25 17:46
antonio_jiao
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-6
收藏
得分:0 
请教,finally块是哪里啊?
2007-07-25 20:56
可可熊
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:553
专家分:0
注 册:2007-6-15
收藏
得分:0 
catch (Exception ee) {
ee.printStackTrace();

这个之后。
2007-07-25 20:58
antonio_jiao
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-6
收藏
得分:0 
好像貌似不行诶,我把这三句都注释掉,这个异常还是存在
2007-07-25 22:19
antonio_jiao
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2007-7-6
收藏
得分:0 
catch (Exception ee) {
ee.printStackTrace();
}finally{
if (rs!= null) rs.close();
if (sm!= null) sm.close();
if (con!= null) con.close();
}
我加了以下语句,改了一下,但是报了新的错误,运行都通不过
"BuyFrame.java": variable rs might not have been initialized at line 147, column 17
"BuyFrame.java": variable rs might not have been initialized at line 148, column 17
"BuyFrame.java": variable rs might not have been initialized at line 149, column 17
"BuyFrame.java": unreported exception java.sql.SQLException; must be caught or declared to be thrown at line 147, column 36
"BuyFrame.java": unreported exception java.sql.SQLException; must be caught or declared to be thrown at line 148, column 36
"BuyFrame.java": unreported exception java.sql.SQLException; must be caught or declared to be thrown at line 149, column 36

也就是这3句话报错
2007-07-25 22:27
witchery
Rank: 1
来 自:西安
等 级:新手上路
帖 子:205
专家分:0
注 册:2005-8-6
收藏
得分:0 
你把rs=sm.executeQuery("select * from TrainTicket");
改成ResultSet rs=sm.executeQuery("select * from TrainTicket");
把上面的rs 的声明去掉..

这么长的代码,楼主怎么不加注释....
2007-07-27 10:17
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你在关闭了以后再用它,当然会抛这个异常啦


可惜不是你,陪我到最后
2007-07-27 20:27
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你最好是把详细的异常信息帖出来

可惜不是你,陪我到最后
2007-07-27 20:29
快速回复:求教一下java.sql.SQLException: ResultSet is closed问题该如何解决
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032525 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved