⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trainticketdao.java

📁 一个模拟订购火车票的小程序 jbuilder2005 开发 MS2000 做数据库
💻 JAVA
字号:
package test;

import java.sql.*;

public class TrainTicketDao {
    private String trainNo;
    private int seatNum;
    private int bedNum;
    private float seatPrice;
    private float bedPrice;
    Connection con;
    /**
     *
     * @param type String 代表订购车票的类型
     * @param qty int 代表订购数量
     */

    void updateKucheng(String type, int qty) {
        this.openCon();
        try {
            java.sql.PreparedStatement pstmt = con.prepareStatement(
                    "update TrainTicket set " + type + "=" + type + "-" + qty +
                    " where trainNo =?");
            pstmt.setString(1, trainNo);

//            java.sql.PreparedStatement pstmt = con.prepareStatement(
//                    "update TrainTicket set ?=?-? where trainNo =?");
//            pstmt.setString(1, type);
//            pstmt.setString(2, type);
//            pstmt.setInt(3, qty);
//            pstmt.setString(4, trainNo);

            pstmt.execute();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        this.closeCon();
    }


    boolean selectAll() { //根据车次查询所有信息
        boolean flag = false;
        this.openCon();
        try {
            java.sql.PreparedStatement pstmt = con.prepareStatement(
                    "select * from TrainTicket where trainNo =?");
            pstmt.setString(1, trainNo);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                flag = true;
                this.setSeatNum(rs.getInt("seatNum"));
                this.setBedNum(rs.getInt("bedNum"));
                this.setSeatPrice(rs.getFloat("seatprice"));
                this.setBedPrice(rs.getFloat("bedPrice"));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        this.closeCon();
        return flag;
    }


    void closeCon() {
        try {
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    void openCon() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();

        }
        try {
            con = DriverManager.getConnection("jdbc:odbc:y21");
        } catch (SQLException ex1) {
            ex1.printStackTrace();
        }

    }

    public String getTrainNo() {
        return trainNo;
    }

    public int getSeatNum() {
        return seatNum;
    }

    public int getBedNum() {
        return bedNum;
    }

    public float getSeatPrice() {
        return seatPrice;
    }

    public float getBedPrice() {
        return bedPrice;
    }

    public void setTrainNo(String trainNo) {
        this.trainNo = trainNo;
    }

    public void setSeatNum(int seatNum) {
        this.seatNum = seatNum;
    }

    public void setBedNum(int bedNum) {
        this.bedNum = bedNum;
    }

    public void setSeatPrice(float seatPrice) {
        this.seatPrice = seatPrice;
    }

    public void setBedPrice(float bedPrice) {
        this.bedPrice = bedPrice;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -