请教一下,最近在做的一道题目,功能基本实现了,但是结束后,跳出异常
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);
}
}