基于java+mysql的swing+mysql火车票售票管理系统(java+swing+mysql+gui)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
管理员管理车票和用户、用户购买车票等
技术框架
Java Swing MySQL
基于java的Swing+MySQL火车票售票管理系统(java+swing+mysql+gui)
return userticketidentity_number;
}
public void setUserticketidentity_number(String userticketidentity_number) {
this.userticketidentity_number = userticketidentity_number;
}
public void setUserticketname(String userticketname) {
this.userticketname = userticketname;
}
public String getUserticketstation_arrival() {
return userticketstation_arrival;
}
public void setUserticketstation_arrival(String userticketstation_arrival) {
this.userticketstation_arrival = userticketstation_arrival;
}
public String getUserticketstation_depart() {
return userticketstation_depart;
}
public void setUserticketstation_depart(String userticketstation_depart) {
this.userticketstation_depart = userticketstation_depart;
}
public String getUserticketdepart_date() {
return userticketdepart_date;
}
public void setUserticketdepart_date(String userticketdepart_date) {
this.userticketdepart_date = userticketdepart_date;
}
public String getUserticketdepart_time() {
return userticketdepart_time;
}
public void setUserticketdepart_time(String userticketdepart_time) {
this.userticketdepart_time = userticketdepart_time;
}
public int getUserticketticket_number() {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, yuan);
stmt.setInt(2, userid);
int result = stmt.executeUpdate();
ResultSet rr = null;
System.out.println(sql);
if (result >= 1) {
flag = true;
}
Util.closeAll(con, stmt, rr);
return flag;
}
@Override
public boolean reduBlance(int userid, int yuan) throws SQLException {
System.out.println(userid + "+++++" + yuan);
boolean flag = false;
String sql = "update user set balance=balance-? where userid = ?";
Connection con = Util.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, yuan);
stmt.setInt(2, userid);
int result = stmt.executeUpdate();
ResultSet rr = null;
System.out.println(sql);
if (result >= 1) {
flag = true;
}
Util.closeAll(con, stmt, rr);
return flag;
}
@Override
public int selectbl(int userid) throws SQLException {
Connection con = Util.getConnection();
int far = 0;
String sqladd = "select balance from user where userid=? ";
PreparedStatement stmt = con.prepareStatement(sqladd);
stmt.setInt(1, userid);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
far = rs.getInt(1);
}
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));//流式居中
yhQue = new JButton("查看用户信息");
panel.add(yhQue);
yhQue.setActionCommand("que");
yhDel = new JButton("删除所选");
panel.add(yhDel);
yhDel.setActionCommand("del");
yhAdd = new JButton("添加用户");
panel.add(yhAdd);
yhAdd.setActionCommand("add");
yhUpdata = new JButton("修改用户");
panel.add(yhUpdata);
yhUpdata.setActionCommand("updata");
yuchonfzhi = new JButton("充值");
panel.add(yuchonfzhi);
yuchonfzhi.setActionCommand("chongzhi");
tzcp = new JButton("用户管理");
panel.add(tzcp);
tuichu = new JButton("退出");
panel.add(tuichu);
tuichu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
public class UpdataTicket extends JFrame {
JPanel contentPane;
private JTextField txtstation_arrival;
private JTextField txtstation_depart;
private JTextField txtdepart_date;
private JTextField txtdepart_time;
private JTextField txtticket_number;
private JTextField txttickets_left;
private JTextField txtfares;
private JTextField txtticket_office;
private JComboBox cmbtickettype;
private JComboBox cmbseattype;
private TickeDao ticketDao;
private TickettypeImpl tickettypeDao;
private SeattypeImpl seattypeDao;
/**
* Create the panel.
*
* @param tickettypeDao
* @throws SQLException
*/
public UpdataTicket(Ticket ticket) throws SQLException {
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 100, 800, 800);
contentPane = new JPanel();
JLabel label = new JLabel("到达站:");
label.setBounds(280, 65, 146, 23);
add(label);
txtstation_arrival = new JTextField();
txtstation_arrival.setBounds(350, 65, 146, 23);
add(txtstation_arrival);
txtstation_arrival.setColumns(10);
txtstation_arrival.setText(ticket.getStation_arrival());
JLabel label_2 = new JLabel("账号");
label_2.setBounds(250, 240, 70, 23);
add(label_2);
txtaccout = new JTextField();
txtaccout.setBounds(350, 240, 180, 23);
add(txtaccout);
txtaccout.setColumns(10);
txtaccout.setText(user.getAccount());
JLabel label_3 = new JLabel("密码");
label_3.setBounds(250, 310, 70, 23);
add(label_3);
txtPassword = new JTextField();
txtPassword.setBounds(350, 310, 180, 23);
add(txtPassword);
txtPassword.setColumns(10);
txtPassword.setText(user.getPassword());
userDao = new UserDaoImpl();
JButton btnNewButton = new JButton("修改用户");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (txtname.getText().equals("")) { //判断用户输入是否为空;
JOptionPane.showMessageDialog(null, "请输入姓名", "警告对话框", JOptionPane.WARNING_MESSAGE);
return;
}
if (txtidentity_number.getText().equals("")) { //判断用户输入是否为空;
JOptionPane.showMessageDialog(null, "请输入身份证号", "警告对话框", JOptionPane.WARNING_MESSAGE);
return;
}
if (txtaccout.getText().equals("")) { //判断用户输入是否为空;
JOptionPane.showMessageDialog(null, "请输入账号", "警告对话框", JOptionPane.WARNING_MESSAGE);
return;
}
if (txtPassword.getText().equals("")) { //判断用户输入是否为空;
JOptionPane.showMessageDialog(null, "请输入密码", "警告对话框", JOptionPane.WARNING_MESSAGE);
return;
}
return;
}
try {
boolean flag;
int userid = id;
String name = txtname.getText();
String identity_number = txtidentity_number.getText();
String account = txtaccout.getText();
String password = txtPassword.getText();
if (account.length() < 1) {
JOptionPane.showMessageDialog(null, "账号必须大于0位!");
txtname.setText("");
txtidentity_number.setText("");
txtaccout.setText("");
txtPassword.setText("");
return;
}
if (password.length() < 1) {
JOptionPane.showMessageDialog(null, "添密码必须大于0位!");
txtname.setText("");
txtidentity_number.setText("");
txtaccout.setText("");
txtPassword.setText("");
return;
}
User user1 = new User(userid, name, identity_number, account, password);
flag = userDao.update(user1);
try {
if (flag) {
JOptionPane.showMessageDialog(null, "修改成功");
txtname.setText("");
txtidentity_number.setText("");
txtaccout.setText("");
txtPassword.setText("");
dispose();
} else {
JOptionPane.showMessageDialog(null, "修改失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public class UpdataTicket extends JFrame {
JPanel contentPane;
private JTextField txtstation_arrival;
private JTextField txtstation_depart;
private JTextField txtdepart_date;
private JTextField txtdepart_time;
private JTextField txtticket_number;
private JTextField txttickets_left;
private JTextField txtfares;
private JTextField txtticket_office;
private JComboBox cmbtickettype;
private JComboBox cmbseattype;
private TickeDao ticketDao;
private TickettypeImpl tickettypeDao;
private SeattypeImpl seattypeDao;
/**
* Create the panel.
*
* @param tickettypeDao
* @throws SQLException
*/
public UpdataTicket(Ticket ticket) throws SQLException {
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 100, 800, 800);
contentPane = new JPanel();
JLabel label = new JLabel("到达站:");
label.setBounds(280, 65, 146, 23);
add(label);
txtstation_arrival = new JTextField();
txtstation_arrival.setBounds(350, 65, 146, 23);
add(txtstation_arrival);
txtstation_arrival.setColumns(10);
stmt.setInt(2, userid);
int result = stmt.executeUpdate();
ResultSet rr = null;
System.out.println(sql);
if (result >= 1) {
flag = true;
}
Util.closeAll(con, stmt, rr);
return flag;
}
@Override
public boolean reduBlance(int userid, int yuan) throws SQLException {
System.out.println(userid + "+++++" + yuan);
boolean flag = false;
String sql = "update user set balance=balance-? where userid = ?";
Connection con = Util.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, yuan);
stmt.setInt(2, userid);
int result = stmt.executeUpdate();
ResultSet rr = null;
System.out.println(sql);
if (result >= 1) {
flag = true;
}
Util.closeAll(con, stmt, rr);
return flag;
}
@Override
public int selectbl(int userid) throws SQLException {
Connection con = Util.getConnection();
int far = 0;
String sqladd = "select balance from user where userid=? ";
PreparedStatement stmt = con.prepareStatement(sqladd);
stmt.setInt(1, userid);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
far = rs.getInt(1);
}
return far;
}
}
/**
}
public int getUserticketticket_number() {
return userticketticket_number;
}
public void setUserticketticket_number(int userticketticket_number) {
this.userticketticket_number = userticketticket_number;
}
public String getUserticketseattype() {
return userticketseattype;
}
public void setUserticketseattype(String userticketseattype) {
this.userticketseattype = userticketseattype;
}
public String getUsertickettickettype() {
return usertickettickettype;
}
public void setUsertickettickettype(String usertickettickettype) {
this.usertickettickettype = usertickettickettype;
}
public String getUserticketfares() {
return userticketfares;
}
public void setUserticketfares(String userticketfares) {
this.userticketfares = userticketfares;
}
public String getUserticketticket_office() {
return userticketticket_office;
}
public void setUserticketticket_office(String userticketticket_office) {
this.userticketticket_office = userticketticket_office;
}
@Override
public String toString() {
return "Userticket [userticketid=" + userticketid + ", userticketuserid=" + userticketuserid
+ ", userticketticketid=" + userticketticketid + ", userticketname=" + userticketname
+ ", userticketidentity_number=" + userticketidentity_number + ", userticketstation_arrival="
+ userticketstation_arrival + ", userticketstation_depart=" + userticketstation_depart
+ ", userticketdepart_date=" + userticketdepart_date + ", userticketdepart_time="
+ userticketdepart_time + ", userticketticket_number=" + userticketticket_number
+ ", userticketseattype=" + userticketseattype + ", usertickettickettype=" + usertickettickettype
+ ", userticketfares=" + userticketfares + ", userticketticket_office=" + userticketticket_office + "]";
}
}
public void setnameField(JTextField c) {
textname = c;
}
public void setidentity_numberField(JTextField f) {
textidentity_number = f;
}
public void setokButton(JButton b1) {
okButton = b1;
}
public void setresetButton(JButton b2) {
resetButton = b2;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (textacc.getText().equals("")) //判断用户输入是否为空;
JOptionPane.showMessageDialog(null, "请输入账号", "警告对话框", JOptionPane.WARNING_MESSAGE);
else if (textpass.getText().equals(""))
JOptionPane.showMessageDialog(null, "请输入密码", "警告对话框", JOptionPane.WARNING_MESSAGE);
else if (textname.getText().equals(""))
JOptionPane.showMessageDialog(null, "请输入姓名", "警告对话框", JOptionPane.WARNING_MESSAGE);
else if (textidentity_number.getText().equals(""))
JOptionPane.showMessageDialog(null, "请输入身份证号", "警告对话框", JOptionPane.WARNING_MESSAGE);
else {
String acc = textacc.getText();
String pass = textpass.getText();
String name = textname.getText();
String identity_number = textidentity_number.getText();
if (acc.trim().length() < 1) {
JOptionPane.showMessageDialog(null, "账号必须大于0位!");
return;
}
if (pass.trim().length() < 1) {
JOptionPane.showMessageDialog(null, "密码必须大于0位!");
return;
}
try {
userdao = new UserDaoImpl();
boolean flags = userdao.SelectAc(acc);
*/
public class Userregister_db extends JFrame implements ActionListener {
JTextField textacc, textpass, textname, textidentity_number;
JButton okButton, resetButton;
Statement stmt;
ResultSet rs;
UserDao userdao;
// double acc;
// String name;
// Connection con = null;
public void setaccountField(JTextField a) {
textacc = a;
}
public void setpassField(JTextField b) {
textpass = b;
}
public void setnameField(JTextField c) {
textname = c;
}
public void setidentity_numberField(JTextField f) {
textidentity_number = f;
}
public void setokButton(JButton b1) {
okButton = b1;
}
public void setresetButton(JButton b2) {
resetButton = b2;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okButton) {
if (textacc.getText().equals("")) //判断用户输入是否为空;
JOptionPane.showMessageDialog(null, "请输入账号", "警告对话框", JOptionPane.WARNING_MESSAGE);
else if (textpass.getText().equals(""))
, seattype, tickettype, tickets_left, fares, ticket_office);
try {
ticketimpl.update(ticket);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
loadData();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public void loadData() throws SQLException {
// 清除旧的数据
model.getDataVector().clear();
List<Ticket> list = ticketDao.queryAll();
// 遍历每一条数据,添加到model中
for (Ticket ticket : list) {
// 添加 行数据
// cob.setSelectedItem(new Tickettype(ticket.getTicketid()));
model.addRow(new Object[]{ticket.getTicketid(), ticket.getStation_arrival(), ticket.getStation_depart(),
ticket.getDepart_date(), ticket.getDepart_time(), ticket.getTicket_number(), ticket.getSeattypeid(),
ticket.getTickettypeid(), ticket.getTickets_left(), ticket.getFares(),
ticket.getTicket_office()
});
}
}
public void del() throws SQLException {
if (table.getSelectedRowCount() <= 0) {
JOptionPane.showMessageDialog(null, "请选择要删除的数据行");
return;
}
int result = JOptionPane.showConfirmDialog(null, "是否确定要删除");
// 判断用户是否点击
if (result == JOptionPane.OK_OPTION) {
int ticketid = Integer.valueOf(table.getValueAt(
table.getSelectedRow(), 0).toString());
ticketDao.delete(ticketid);
loadData();
public class Userbuy_db extends JPanel {
private JTable table;
private DefaultTableModel model;// 用于存储表格数据
private String oldValue = "";// 保存单元格编辑 前的值
private TickeDao ticketDao;
private TickeDao ticketim;
private TickettypeImpl tickettypeDao;
private UserTicketDao userticketdao;
private UserDao userdao;
JPanel contentPane;
JPanel contentPane4;
JPanel panelBottom;
public String sql;
Statement stmt;
public Userbuy_db() {
setLayout(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane();
add(scrollPane, BorderLayout.CENTER);
table = new JTable();
scrollPane.setColumnHeaderView(table);
// 初始化存储表格数据的对象
model = new DefaultTableModel(new Object[][]{}, new String[]{"车票编号",
"到达站", "出发站", "发车日期", "发车时间", "车次", "座位类型", "机车类型", "余票", "票价", "售票站"});
// 将数据绑定到对象中
table.setModel(model);
table.setRowHeight(30);
scrollPane.setViewportView(table);
tickettypeDao = new TickettypeImpl();
ticketDao = new TicketImpl();
try {
loadData();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// 为表格绑定修改值后的事件
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getColumn() < 0)
return;
}
public String getTickettypeid() {
return tickettypeid;
}
public void setTickettypeid(String tickettypeid) {
this.tickettypeid = tickettypeid;
}
public int getTickets_left() {
return tickets_left;
}
public void setTickets_left(int tickets_left) {
this.tickets_left = tickets_left;
}
public String getFares() {
return fares;
}
public void setFares(String fares) {
this.fares = fares;
}
public String getTicket_office() {
return ticket_office;
}
public void setTicket_office(String ticket_office) {
this.ticket_office = ticket_office;
}
@Override
public String toString() {
return "Ticket [ticketid=" + ticketid + ", station_arrival=" + station_arrival + ", station_depart="
+ station_depart + ", depart_date=" + depart_date + ", depart_time=" + depart_time + ", ticket_number="
+ ticket_number + ", seattypeid=" + seattypeid + ", tickettypeid=" + tickettypeid + ", tickets_left="
+ tickets_left + ", fares=" + fares + ", ticket_office=" + ticket_office + "]";
}
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(320, 400, 116, 23);
add(btnNewButton);
}
}
/**
* 管理员操作界面
*/
public class Ticketmanagement_ui extends JFrame implements ActionListener {
JPanel contentPane4;
JPanel contentPane;
JPanel panelBottom;
private JButton tzyh;
private JButton btnQue;
private JButton btnDel;
private JButton btnAdd;
private JButton tuichu;
private JButton btnUpd;
private AddTicket add;
private QueryTicket query;
public Ticketmanagement_ui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 100, 800, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
public class UserTicketImpl implements UserTicketDao {
@Override
public boolean add(int userticketuserid, int userticketticketid, String userticketname,
String userticketidentity_number, String userticketstation_arrival, String userticketstation_depart,
String userticketdepart_date, String userticketdepart_time, int userticketticket_number,
String userticketseattype, String usertickettickettype, String userticketfares,
String userticketticket_office) throws Exception {
Connection con = Util.getConnection();
String sqladd = "insert into userticket(userticketuserid,userticketticketid,userticketname,userticketidentity_number,userticketstation_arrival,"
+ "userticketstation_depart,userticketdepart_date,"
+ "userticketdepart_time,userticketticket_number,userticketseattype,"
+ "usertickettickettype,userticketfares,userticketticket_office) values"
+ "(?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(sqladd);
stmt.setInt(1, userticketuserid);
stmt.setInt(2, userticketticketid);
stmt.setString(3, userticketname);
stmt.setString(4, userticketidentity_number);
stmt.setString(5, userticketstation_arrival);
stmt.setString(6, userticketstation_depart);
stmt.setString(7, userticketdepart_date);
stmt.setString(8, userticketdepart_time);
stmt.setInt(9, userticketticket_number);
stmt.setString(10, userticketseattype);
stmt.setString(11, usertickettickettype);
stmt.setString(12, userticketfares);
stmt.setString(13, userticketticket_office);
stmt.executeUpdate();
return true;
}
@Override
public boolean update(UserTicket UserTicket) throws SQLException {
// TODO Auto-generated method stub
return false;
}
@Override
public void delete(int userticketid) throws SQLException {
Connection con = Util.getConnection();
String sqladd = "delete from userticket where userticketid=?";
PreparedStatement stmt = con.prepareStatement(sqladd);
stmt.setInt(1, userticketid);
ResultSet rs = null;
stmt.executeUpdate();
Util.closeAll(con, stmt, rs);
}
add(txtticket_number);
txtticket_number.setColumns(10);
txtticket_number.setText(String.valueOf(ticket.getTicket_number()));
JLabel label_4 = new JLabel("车票余量:");
label_4.setBounds(280, 227, 146, 23);
add(label_4);
txttickets_left = new JTextField();
txttickets_left.setBounds(350, 227, 146, 23);
add(txttickets_left);
txttickets_left.setColumns(10);
txttickets_left.setText(String.valueOf(ticket.getTickets_left()));
tickettypeDao = new TickettypeImpl();
List<Tickettype> list = tickettypeDao.queryAll();
System.out.println(list);
JLabel label_5 = new JLabel("机车类型:");
label_5.setBounds(280, 260, 146, 23);
add(label_5);
cmbtickettype = new JComboBox(list.toArray());
cmbtickettype.setBounds(350, 260, 146, 23);
add(cmbtickettype);
seattypeDao = new SeattypeImpl();
List<Seattype> list1 = seattypeDao.queryAll();
JLabel label_6 = new JLabel("座位类型:");
label_6.setBounds(280, 292, 146, 23);
add(label_6);
cmbseattype = new JComboBox(list1.toArray());
cmbseattype.setBounds(350, 292, 146, 23);
add(cmbseattype);
JLabel label_7 = new JLabel("车票价格:");
label_7.setBounds(280, 325, 146, 23);
add(label_7);
txtfares = new JTextField();
txtfares.setBounds(350, 325, 146, 23);
add(txtfares);
txtfares.setColumns(10);
String seattypeid = table.getValueAt(table.getSelectedRow(), 6).toString();
String tickettypeid = table.getValueAt(table.getSelectedRow(), 7).toString();
int tickets_left = Integer.valueOf(table.getValueAt(table.getSelectedRow(), 8).toString());
String fares = table.getValueAt(table.getSelectedRow(), 9).toString();
String ticket_office = table.getValueAt(table.getSelectedRow(), 10).toString();
Ticket ticket = new Ticket(ticketid, station_arrival, station_depart, depart_date, depart_time,
ticket_number, seattypeid, tickettypeid, tickets_left, fares, ticket_office);
UpdataTicket updataticket = new UpdataTicket(ticket);
updataticket.setVisible(true);
}
}
}
/**
* 车票信息修改
*/
public class UpdataTicket extends JFrame {
JPanel contentPane;
private JTextField txtstation_arrival;
private JTextField txtstation_depart;
private JTextField txtdepart_date;
private JTextField txtdepart_time;
private JTextField txtticket_number;