?? db.java~33~
字號:
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 {
float price = 0;
public DB() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
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;
}
try {
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()) {
tb.setSeatNum(Integer.parseInt(frm.OrderNum.getText()));
if ( rs.getInt("SeatNum") == 0 ||
tb.getSeatNum() > rs.getInt("SeatNum")) {
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()));
if (/*rs.getInt("bedNum") == 0 ||*/
tb.getBedNum() > rs.getInt("bedNum")) {
JOptionPane.showMessageDialog(frm, "訂購失敗:車票數量不足");
return false;
}
tb.setBedPrice(rs.getFloat("bedPrice"));
// System.out.println(tb.getBedPrice()+" "+tb.getBedNum());
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;
}
private void jbInit() throws Exception {
}
////查找硬座
// public int checkseat(TicketBean tb) {
// Connection con = getcon();
// if(String.valueOf(tb.getSeatNum())==null ||tb.getSeatNum()<=0)
// return 2;
// try {
//
// PreparedStatement s = con.prepareStatement(
// "select * from Train where TrainNo = ?");
// s.setString(1, tb.getTrainNo());
// ResultSet rs = s.executeQuery();
// if (rs.next()) {
// if (tb.getSeatNum() >= rs.getInt("SeatNum")) {
// tb.setSeatPrice(rs.getFloat("seatPrice"));
// return 1;
// }
// }
// } catch (SQLException ex) {
// ex.printStackTrace();
// }
//
// this.close(con);
//
// return 0;
// }
//
////查找硬臥
// public int checkbed(TicketBean tb) {
// Connection con = getcon();
// if(String.valueOf(tb.getBedNum())==null ||tb.getBedNum()<=0)
// return 2;
// try {
//
// PreparedStatement s = con.prepareStatement(
// "select * from Train where TrainNo = ?");
// s.setString(1, tb.getTrainNo());
// ResultSet rs = s.executeQuery();
// if (rs.next()) {
// if (tb.getBedNum() >= rs.getInt("Bednum")) {
// tb.setBedPrice(rs.getFloat("bedPrice"));
// return 1;
// }
// }
//
// } catch (SQLException ex) {
// ex.printStackTrace();
// }
//
// this.close(con);
//
// return 0;
// }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -