Java+Mysql 实现 车票预定系统

前言

基于Javafx+Mysql实现的车票管理系统。
界面包括:用户登录界面、班车查询界面、车票预订界面。
用户需要注册登录账号,然后可以根据起始站和终点站以及时间查询该天内符合条件的班次汽车,然后可以根据余票是否充足来预定车票。
文章结尾有完整源代码展示!
在这里插入图片描述

一、页面展示

1.登录页面

在这里插入图片描述

2.注册页面

在这里插入图片描述

3.车票预览总页面

在这里插入图片描述

4.车票预定页面

在这里插入图片描述

二、数据库连接

public class DBHelp {
    public static final String URL="jdbc:mysql://127.0.0.1:3306/unite?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    public static final String USER="root";//用户名
    public static final String PASSWORD="123";//密码
    Connection con=DriverManager.getConnection(URL, USER, PASSWORD); 
// 接着执行 sql语句
PreparedStatement pst =con.prepareStatement(sql) ;
// 查看 sql语句的执行结果
ResultSet rs = pstexecuteQuery();
}

三、代码分析

1.预定车票

     Student row =  table.getSelectionModel().getSelectedItem();
                if (row == null){
                    warn warn = new warn(stage,"请选择您要预定的车次");
                }else {


                Connection connection = null;
                try {
                    connection = DriverManager.getConnection(DBHelp.URL,DBHelp.USER,DBHelp.PASSWORD);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                PreparedStatement preparedStatement = null;

                try {
                    preparedStatement = connection.prepareStatement("select realname ,user_num from zhuce where user_num =? " );
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    preparedStatement.setString(1,sss);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                ResultSet resultSet = null;
                try {
                    resultSet = preparedStatement.executeQuery();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }


                String s = row.getTimeyear()+"-"+row.getTimemonth()+"-"+row.getTimeday()+" "+row.getTime();

                String s1 = row.getOrigin();
                String s2 =row.getEnd();
                String amount = row.getAmount();
                String id = row.getId();


                Booking booking = new Booking(stage,ss,sss,s1,s2,s,amount,id);
                  int a = Integer.parseInt(amount);
                if (a<=0){
                    warn warn = new warn(stage2,"        余票不足!");
                }else {
                    a--;

                    warn warn = new warn(stage2,"        购票成功!");

                    Connection connection = null;
                    try {
                        connection = DriverManager.getConnection(DBHelp.URL,DBHelp.USER,DBHelp.PASSWORD);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    PreparedStatement preparedStatement = null;

                    try {

                        preparedStatement = connection.prepareStatement("update information set amount=? where id=?" );
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                    try {
                        preparedStatement.setString(1,String.valueOf(a));
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    try {
                         preparedStatement.setString(2,idd);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }

                    try {
                        preparedStatement.executeUpdate();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }


                    stage2.close();
                    stage.close();
                    Mywindow mywindow = new Mywindow(s1,s);

2.重置信息

                c1.setValue("东校区");
                c2.setValue("西校区");
                c3.setValue("2019");
                c4.setValue("01");
                c5.setValue("01");
                String sql3 = "SELECT id,time,price ,amount,timeyear,timemonth,timeday,origin,end  from information order by ID ASC";

                try {
                    leader_log.mywindow.date(table,sql3);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();

                }

3.登录账号


                Connection con = null;//

                try {
                    con = DriverManager.getConnection(DBHelp.URL, DBHelp.USER, DBHelp.PASSWORD);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                String sql = "select user_num,password,realname from zhuce where user_num=? and password=?";
                PreparedStatement ptmt = null;
                try {
                    ptmt = con.prepareStatement(sql);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    ptmt.setString(1, text.getText());
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    ptmt.setString(2, pass.getText());
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                ResultSet rs = null;
                try {
                    rs = ptmt.executeQuery();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                //从登录用户给出的账号密码来检测查询在数据库表中是否存在相同的账号密码
                try {
                    if (rs.next()) {
//                       System.out.println("登录成功!");
                        String s9=rs.getString(3);

                        mywindow=new Mywindow(text.getText(),s9);
                        stage1.close();
                    } else {
//                       System.out.println("密码错误!");
                        denglushibai shibai = new denglushibai(stage1);
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

4.注册账号

 AnchorPane zhuce = new AnchorPane();
                Stage zhuce1 = new Stage();
                zhuce1.initOwner(stage1);
                zhuce1.initModality(Modality.WINDOW_MODAL);
                Scene zhuce2 = new Scene(zhuce);

                zhuce1.setScene(zhuce2);
                Text text1 = new Text(148,118,"欢迎注册");
                text1.setFont(Font.font(40));

                TextField text2 = new TextField();
                text2.setPromptText("请输入用户名");
                text2.setLayoutX(140);
                text2.setLayoutY(230);

                TextField text3 = new TextField();
                text3.setPromptText("请输入您的真实姓名");
                text3.setLayoutX(140);
                text3.setLayoutY(170);


                PasswordField pass1 = new PasswordField();
                pass1.setLayoutX(140);
                pass1.setLayoutY(290);
                pass1.setPromptText("请输入你要设置的密码");
                Button button1 = new Button("立即注册");
                button1.setLayoutX(190);
                button1.setLayoutY(350);


                //   点击注册后的 判断
                button1.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {


                        //  判断 能不能 注册成功
                        if (text2.getText().isEmpty() || pass1.getText().isEmpty() || text3.getText().isEmpty()) {  // 判断输入内容是否为空
                                warn shibai = new warn(stage1,"请输入完整的信息");

                        } else if (text2.getText().indexOf(" ")!=-1||pass1.getText().indexOf(" ")!=-1 ||text3.getText().indexOf(" ")!=-1){
                                warn warn = new warn(stage1,"请勿输入非法字符");
                        }else {

                            // 判断是否 重复创建账号-------------
                            Connection con=null;
                            try {
                                con=DriverManager.getConnection(DBHelp.URL,DBHelp.USER,DBHelp.PASSWORD);
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }
                            String sql = "select user_num from zhuce where user_num=? ";
                            PreparedStatement ptmt = null;
                            try {
                                ptmt=con.prepareStatement(sql);
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }
                            try {
                                ptmt.setString(1,text2.getText());
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }

                            ResultSet rs=null;
                            try {
                                rs=ptmt.executeQuery();
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }
                            try {
                                if (rs.next()) {  // 账号已经存在
                                   warn shibai3 = new warn(stage1,"您输入的账号已经存在");
                                }else {  //   账号不存在,可以注册
                                    zhuce1.close();
                                    zhucechenggong zhu = new zhucechenggong(stage1);

//   增加账号模块
//--------------------------------------------------------------------------------------------------------
                                    PreparedStatement pst = null;

                                    try {
                                        //1.加载驱动程序
                                        //此语句固定,使用MySQL数据库无需更改,在JSP中可不加异常处理
                                        try {
                                            Class.forName("com.mysql.cj.jdbc.Driver");
                                        } catch (ClassNotFoundException e) {
                                            e.printStackTrace();
                                        }
                                        //2.获取数据库的连接
                                        //此语句只需更改端口、数据库名称、用户名及密码,使用MySQL数据库格式固定,在JSP中可不加异常处理

                                        con = DriverManager.getConnection(DBHelp.URL, DBHelp.USER, DBHelp.PASSWORD);
                                        //3.通过数据库的连接操作数据库,实现新增数据
                                        Statement sql1 = con.createStatement();

                                        try {
                                            con = DriverManager.getConnection(DBHelp.URL, DBHelp.USER, DBHelp.PASSWORD);
                                            pst = con.prepareStatement("insert into zhuce (user_num,password,realname) values(?,?,?)");
                                            pst.setString(1, text2.getText());
                                            pst.setString(2, pass1.getText());
                                            pst.setString(3, text3.getText());
                                            pst.execute();
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }

                                    } catch (SQLException E) {
                                        System.out.println("SQL异常!!!!");
                                    }

                                }
                            } catch (SQLException throwables) {
                                throwables.printStackTrace();
                            }


                        }
//--------------------------------------------------------------------------------------------------------

                    }
                });

                zhuce.getChildren().addAll(text1,text2,pass1,button1,text3);

                zhuce1.setTitle("Sign Form");
                zhuce1.setWidth(500);
                zhuce1.setHeight(600);
                zhuce1.setResizable(false);
                zhuce1.show();

四、完整代码展示

由于代码过长,所以本文展示部分的代码,完整代码在下方的百度网盘里,有需要的可以自行提取
链接:https://pan.baidu.com/s/19CfLj6yRITZZuo4F38zMrA?pwd=ojbk
提取码:ojbk
有任何问题的可以再评论区指出!

猜你喜欢

转载自blog.csdn.net/m0_63512120/article/details/130276660