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

📄 db.java~31~

📁 火车票的订购系统,实现简单的查阅,定购等功能
💻 JAVA~31~
字号:
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.setBedPrice(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 + -