📄 db.java
字号:
package trainticket;
import java.sql.*;
import javax.swing.JOptionPane;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DB {
Frame1 frm = new Frame1();
public DB(Frame1 frm) {
this.frm = frm;
}
//得到连接
public Connection getcon() {
Connection con = null;
String url = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url = "jdbc:odbc:netbar";
con = DriverManager.getConnection(url);
} catch (SQLException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return con;
}
//关闭连接
public void close(Connection con) {
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//查找车次,一系列判断
public boolean find(TicketBean tb) {
Connection con = this.getcon();
//判断数据库是否连接
if (con == null) {
JOptionPane.showMessageDialog(frm, "数据库连接失败");
return false;
}
//是否输入车次
if (frm.TrainNum.getText() == null ||
frm.TrainNum.getText().length() <= 0) {
JOptionPane.showMessageDialog(frm, "请输入车次");
return false;
}
//是否选择乘车方式
if (frm.RaidaoHard.isSelected() == false && frm.RadioSoft.isSelected() == false) {
JOptionPane.showMessageDialog(frm, "请选择乘车方式");
return false;
}
//是否填写购买数量
if (frm.OrderNum.getText() == null ||
frm.OrderNum.getText().length() <= 0) {
JOptionPane.showMessageDialog(frm, "请填写购买数量");
return false;
}
//数据库结果集里rs.*只能被调用一次,所以为了多次调用而把rs.*依次赋给相同类型的变量
try {
float price = 0;
int snum = 0;
int bnum = 0;
PreparedStatement s = con.prepareStatement(
"select * from Train where TrainNo = ? ");
s.setString(1, tb.getTrainNo());
ResultSet rs = s.executeQuery();
if (rs.next()) {
if (rs.getString("TrainNo").trim().equals(tb.getTrainNo())) {
if (frm.RaidaoHard.isSelected()) {
snum = rs.getInt("SeatNum");
tb.setSeatNum(Integer.parseInt(frm.OrderNum.getText()));
if (snum <= 0 ||
tb.getSeatNum() > snum) {
JOptionPane.showMessageDialog(frm, "订购失败:车票数量不足");
return false;
}
tb.setSeatPrice(rs.getFloat("seatPrice"));
price = tb.getSeatPrice() *
tb.getSeatNum();
JOptionPane.showMessageDialog(frm,
"车票订购成功,请支付" +
price + "元");
return true;
}
if (frm.RadioSoft.isSelected()) {
tb.setBedNum(Integer.parseInt(frm.OrderNum.getText()));
bnum =rs.getInt("bedNum");
if ( bnum == 0 ||
tb.getBedNum() > bnum) {
JOptionPane.showMessageDialog(frm, "订购失败:车票数量不足");
return false;
}
tb.setBedPrice(rs.getFloat("bedPrice"));
price = tb.getBedPrice() *
tb.getBedNum();
JOptionPane.showMessageDialog(frm,
"车票订购成功,请支付" +
price
+ "元");
return true;
}
} else {
JOptionPane.showMessageDialog(frm, "无此车次");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
this.close(con);
return false;
}
public void update(TicketBean tb){
Connection con = this.getcon();
try {
if(frm.RaidaoHard.isSelected()){
tb.setSeatNum(Integer.parseInt(frm.OrderNum.getText()));
PreparedStatement sseat = con.prepareStatement(
"update Train set seatNum = seatNum - ? where TrainNo =?");
sseat.setInt(1, tb.getSeatNum());
sseat.setString(2, tb.getTrainNo());
sseat.executeUpdate();
tb.setSeatNum(Integer.parseInt(frm.OrderNum.getText()));
}
if(frm.RadioSoft.isSelected()){
PreparedStatement bseat = con.prepareStatement(
"update Train set bedNum = bedNum - ? where TrainNo =?");
bseat.setInt(1,tb.getBedNum());
bseat.setString(2,tb.getTrainNo());
bseat.executeUpdate();
}
} catch (SQLException ex) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -