?? db.java~38~
字號:
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, "訂購失?。很嚻睌盗坎蛔?quot;);
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, "訂購失?。很嚻睌盗坎蛔?quot;);
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();
PreparedStatement s = con.prepareStatement("update Train set seatNum = seatNum - 1 where TrainNo ='T105'");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -