📄 model.java
字号:
package test;
import java.sql.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Model {
private String trainNo;
private String seatNum;
private String bedNum;
private String seatPrice;
private String bedPrice;
private String temp;
Connection con;
private String tempNum;
boolean selecttrainNo(){//查找车次
this.openCon();
boolean flag=false;
String sql="select * from TrainTicket where trainNo=?";
try {
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1,trainNo);
ResultSet rs=pstmt.executeQuery();//执行
if(rs.next()){
this.setSeatNum(rs.getString(2)); //接受该车次信息
this.setBedNum(rs.getString(3));
this.setSeatPrice(rs.getString(4));
this.setBedPrice(rs.getString(5));
flag=true;
}
this.closeCon();
} catch (SQLException ex) {
ex.printStackTrace();
flag=false;
}
return flag;
}
void update(){//订购成功修改该车次信息
this.openCon();
String sqlseat="update TrainTicket set seatNum=seatNum-? where TrainNo=?";//硬座
String sqlbed="update TrainTicket set bedNum=bedNum-? where TrainNo=?";//硬卧
if(this.getTemp().equals("硬座")){//如果用户选择的乘车方式是硬座
try {
PreparedStatement pstmt = con.prepareStatement(sqlseat);
pstmt.setInt(1,Integer.parseInt(tempNum));
pstmt.setString(2,trainNo);
pstmt.execute();//执行SQL语句
} catch (SQLException ex) {
ex.printStackTrace();
}
}
if(this.getTemp().equals("硬卧")){//如果用户选择的乘车方式是硬卧
try {
PreparedStatement pstmt = con.prepareStatement(sqlbed);
pstmt.setInt(1,Integer.parseInt(tempNum));//tempNum是用户输入的,因为seatNum和bedNum在查询时值发生了改变所以另外定义一个来接受值
pstmt.setString(2,trainNo);
pstmt.execute();//执行SQL语句
} catch (SQLException ex) {
ex.printStackTrace();
}
this.closeCon();
}
}
void openCon(){//打开数据库连接
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:odbc:tset");
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
void closeCon(){//关闭流
try {
con.close();
} catch (SQLException ex) {ex.printStackTrace() ;
}
}
public void setTrainNo(String trainNo) {
this.trainNo = trainNo;
}
public void setSeatNum(String seatNum) {
this.seatNum = seatNum;
}
public void setBedNum(String bedNum) {
this.bedNum = bedNum;
}
public void setSeatPrice(String seatPrice) {
this.seatPrice = seatPrice;
}
public void setBedPrice(String bedPrice) {
this.bedPrice = bedPrice;
}
public void setTemp(String temp) {
this.temp = temp;
}
public void setTempNum(String tempNum) {
this.tempNum = tempNum;
}
public String getTrainNo() {
return trainNo;
}
public String getSeatNum() {
return seatNum;
}
public String getBedNum() {
return bedNum;
}
public String getSeatPrice() {
return seatPrice;
}
public String getBedPrice() {
return bedPrice;
}
public String getTemp() {
return temp;
}
public String getTempNum() {
return tempNum;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -