Swing+Mysql实现的火车票管理系统2(功能包含用户注册登录,车票管理、车票添加、站点管理、站点添加、系统介绍等)

Swing+Mysql实现的火车票管理系统2

本系统是一个简单的火车票管理系统,实现了站点和车票的管理。
(文末查看完整源码)

实现功能截图

用户注册登录
请添加图片描述
请添加图片描述
车票管理
请添加图片描述
车票添加
请添加图片描述
站点管理
请添加图片描述
站点添加
请添加图片描述

系统功能

本系统实现了以下功能:
1、用户注册登录
2、车票管理
3、车票添加
4、系统介绍
5、站点管理
6、站点添加

使用技术

数据库:mysql
开发工具:Idea(Myeclispe、Eclipse也可以)
知识点:Swing

项目结构
在这里插入图片描述

代码

java端
实体类
Tricket.java

package com.code2life.model;

import java.sql.Date;

public class Tricket {
    private int id; //车次信息
    private int startID;    //初始站号
    private String startStation;    //初始站名
    private int endID;       //目的站号
    private String endStation;  //目的站名
    private Float price;
    private String seat;    //座位信息
    private Date startTime;    //发车时间
    private Date endTime;      //到达时间
    private String remarks;     //备注

    public Tricket() {
        super();
    }

    public Tricket(int id, String startStation, String endStation, Float price, String seat, Date startTime, Date endTime, String remarks) {
        this.id = id;
        this.startStation = startStation;
        this.endStation = endStation;
        this.price = price;
        this.seat = seat;
        this.startTime = startTime;
        this.endTime = endTime;
        this.remarks = remarks;
    }

    public Tricket(int id , int startID, String startStation, int endID, String endStation, String seat, Float price, Date startTime, Date endTime, String remarks) {
        this.id=id;
        this.startID = startID;
        this.startStation = startStation;
        this.endID = endID;
        this.endStation = endStation;
        this.seat = seat;
        this.price=price;
        this.startTime = startTime;
        this.endTime = endTime;
        this.remarks = remarks;
    }

    public Tricket(int id, String startStation, String endStation) {
        this.id = id;
        this.startStation = startStation;
        this.endStation = endStation;
    }

    public int getId() {
        return id;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStartID() {
        return startID;
    }

    public void setStartID(int startID) {
        this.startID = startID;
    }

    public String getStartStation() {
        return startStation;
    }

    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }

    public int getEndID() {
        return endID;
    }

    public void setEndID(int endID) {
        this.endID = endID;
    }

    public String getEndStation() {
        return endStation;
    }

    public void setEndStation(String endStation) {
        this.endStation = endStation;
    }

    public String getSeat() {
        return seat;
    }

    public void setSeat(String seat) {
        this.seat = seat;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }
}

dao层
TricketDao.java

package com.code2life.dao;

import com.code2life.model.Tricket;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Objects;

public class TricketDao {

    public static int add(Connection con, Tricket tricket) throws Exception{
        String sql ="insert into tricket values(?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setInt(1,tricket.getId());
        pstmt.setInt(2,tricket.getStartID());
        pstmt.setString(3,tricket.getStartStation());
        pstmt.setInt(4,tricket.getEndID());
        pstmt.setString(5,tricket.getEndStation());
        pstmt.setString(6,tricket.getSeat());
        pstmt.setFloat(7,tricket.getPrice());
        pstmt.setDate(8,tricket.getStartTime());
        pstmt.setDate(9,tricket.getEndTime());
        pstmt.setString(10,tricket.getRemarks());
        return pstmt.executeUpdate();
    }

    public ResultSet list(Connection con,Tricket tricket) throws Exception{
        StringBuffer srb= new StringBuffer("select * from tricket b,station_inform c where b.startStation=c.station");
        if(tricket.getId()!=-1 && tricket.getId()!=0){
            srb.append(" and b.id="+tricket.getId());
        }
        if(StringUil.isNotEmpty(tricket.getStartStation()) && !Objects.equals(tricket.getStartStation(), "请选择...")){
            srb.append(" and b.startStation like '%"+tricket.getStartStation()+"%'");
        }
        if(StringUil.isNotEmpty(tricket.getEndStation()) && !Objects.equals(tricket.getEndStation(), "请选择...")){
            srb.append(" and b.endStation like '%"+tricket.getEndStation()+"%'");
        }
        PreparedStatement pstmt = con.prepareStatement(srb.toString());
        //System.out.println(srb);
        return pstmt.executeQuery();
    }
    public int delete(Connection con,String id) throws Exception{
        String sql ="delete from tricket where id=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1,id);
        return pstmt.executeUpdate();
    }
    public static int update(Connection con, Tricket tricket) throws Exception{
        String sql ="update tricket set startStation=?," +
                "endStation=?,seat=?,price=?,startTime=?,arriveTime=?," +
                "remarks=? where id=?";
        PreparedStatement pstmt=con.prepareStatement(sql);
        pstmt.setString(1,tricket.getStartStation());
        pstmt.setString(2,tricket.getEndStation());
        pstmt.setString(3,tricket.getSeat());
        pstmt.setFloat(4,tricket.getPrice());
        pstmt.setDate(5,tricket.getStartTime());
        pstmt.setDate(6,tricket.getEndTime());
        pstmt.setString(7,tricket.getRemarks());
        pstmt.setInt(8,tricket.getId());

        return pstmt.executeUpdate();
    }
}

frame层
loginFrame.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.code2life.view;

import com.code2life.dao.connectDao;
import com.code2life.dao.StringUil;
import com.code2life.dao.UserDao;
import com.code2life.model.User;

import javax.swing.*;
import java.sql.Connection;

/**
 *
 * @author Administrator
 */
public class loginFrame extends javax.swing.JFrame {

    private connectDao conn=new connectDao();
    private UserDao userDao=new UserDao();
    /**
     * Creates new form NewJFrame
     */
    public loginFrame() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        User_name_txt = new javax.swing.JTextField();
        User_password_txt = new javax.swing.JTextField();
        Button_log = new javax.swing.JButton();
        Button_register = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("管理员登录");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("宋体", 1, 24)); // NOI18N
        jLabel1.setText("火车售票系统");
        jLabel1.setToolTipText("");

        jLabel2.setText("用户名:");

        jLabel3.setText("密 码:");

        User_name_txt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                User_name_txtActionPerformed(evt);
            }
        });

        Button_log.setText("登录");
        Button_log.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_logActionPerformed(evt);
            }
        });

        Button_register.setText("注册");
        Button_register.setToolTipText("");
        Button_register.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button_zhongzhiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(80, 80, 80)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(Button_log, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(Button_register, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(83, 83, 83))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18, 18, 18)
                                                                .addComponent(User_password_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18, 18, 18)
                                                                .addComponent(User_name_txt)))
                                                .addGap(69, 69, 69))))
                        .addGroup(layout.createSequentialGroup()
                                .addGap(117, 117, 117)
                                .addComponent(jLabel1)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGap(52, 52, 52)
                                .addComponent(jLabel1)
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(User_name_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(43, 43, 43)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel3)
                                        .addComponent(User_password_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(Button_log, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(Button_register, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(40, 40, 40))
        );

        pack();
    }// </editor-fold>

    private void Button_logActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        String userName=this.User_name_txt.getText();
        String password=this.User_password_txt.getText();
        if(StringUil.isEmpty(userName)) {
            JOptionPane.showMessageDialog(null,"用户名不能为空");
            return;
        }
        if(StringUil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"密码不不能为空");
            return;
        }
        User user=new User(userName,password);
        Connection con=null;
        try {
            User currentUser=userDao.login(conn.getCon(),user);
            if(currentUser!=null){
                dispose();
                new MainFrame().setVisible(true);
            }else{
                JOptionPane.showMessageDialog(null,"用户名或密码错误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                conn.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    private void Button_zhongzhiActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        this.dispose();
        new registerFrame().setVisible(true);
    }
    private void User_name_txtActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new loginFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JButton Button_log;
    private javax.swing.JButton Button_register;
    private javax.swing.JTextField User_name_txt;
    private javax.swing.JTextField User_password_txt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration
}

完整源码

觉得有用,记得一键三连哦!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anmu4200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值