?? db.java
字號(hào):
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;
}
//關(guān)閉連接
public void close(Connection con) {
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
//查找車(chē)次,一系列判斷
public boolean find(TicketBean tb) {
Connection con = this.getcon();
//判斷數(shù)據(jù)庫(kù)是否連接
if (con == null) {
JOptionPane.showMessageDialog(frm, "數(shù)據(jù)庫(kù)連接失敗");
return false;
}
//是否輸入車(chē)次
if (frm.TrainNum.getText() == null ||
frm.TrainNum.getText().length() <= 0) {
JOptionPane.showMessageDialog(frm, "請(qǐng)輸入車(chē)次");
return false;
}
//是否選擇乘車(chē)方式
if (frm.RaidaoHard.isSelected() == false && frm.RadioSoft.isSelected() == false) {
JOptionPane.showMessageDialog(frm, "請(qǐng)選擇乘車(chē)方式");
return false;
}
//是否填寫(xiě)購(gòu)買(mǎi)數(shù)量
if (frm.OrderNum.getText() == null ||
frm.OrderNum.getText().length() <= 0) {
JOptionPane.showMessageDialog(frm, "請(qǐng)?zhí)顚?xiě)購(gòu)買(mǎi)數(shù)量");
return false;
}
//數(shù)據(jù)庫(kù)結(jié)果集里rs.*只能被調(diào)用一次,所以為了多次調(diào)用而把rs.*依次賦給相同類(lèi)型的變量
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, "訂購(gòu)失敗:車(chē)票數(shù)量不足");
return false;
}
tb.setSeatPrice(rs.getFloat("seatPrice"));
price = tb.getSeatPrice() *
tb.getSeatNum();
JOptionPane.showMessageDialog(frm,
"車(chē)票訂購(gòu)成功,請(qǐng)支付" +
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, "訂購(gòu)失敗:車(chē)票數(shù)量不足");
return false;
}
tb.setBedPrice(rs.getFloat("bedPrice"));
price = tb.getBedPrice() *
tb.getBedNum();
JOptionPane.showMessageDialog(frm,
"車(chē)票訂購(gòu)成功,請(qǐng)支付" +
price
+ "元");
return true;
}
} else {
JOptionPane.showMessageDialog(frm, "無(wú)此車(chē)次");
}
}
} 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) {
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -