基于java+mysql的swing+mysql火车票售票管理系统(java+swing+mysql+gui)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
管理员管理车票和用户、用户购买车票等
技术框架
Java Swing MySQL
基于java+mysql的Swing+MySQL火车票售票管理系统(java+swing+mysql+gui)
* 车票信息修改
*/
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);
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;
}
try {
boolean flag;
int userid = user.getUserid();
String name = txtname.getText();
String identity_number = txtidentity_number.getText();
String account = txtaccout.getText();
String password = txtPassword.getText();
if (account.length() == 0) {
JOptionPane.showMessageDialog(null, "账号必须大于0位!");
txtname.setText("");
txtidentity_number.setText("");
txtaccout.setText("");
txtPassword.setText("");
return;
}
if (password.length() == 0) {
JOptionPane.showMessageDialog(null, "添密码必须大于0位!");
txtname.setText("");
txtidentity_number.setText("");
txtaccout.setText("");
public class QueryUser extends JPanel {
private JTable table;
private DefaultTableModel model;// 用于存储表格数据
private UserDao userDao;
private String oldValue = "";// 保存单元格编辑 前的值
/**
* Create the panel.
*/
public QueryUser() {
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[]{"User Id",
"user name", "ID", "User", "Password"});
// 将数据绑定到对象中
table.setModel(model);
table.setRowHeight(30);
scrollPane.setViewportView(table);
userDao = new UserDaoImpl();
loadData();
// 为表格绑定修改值后的事件
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
if (e.getColumn() < 0)
return;
String nVal = table.getValueAt(e.getLastRow(), e.getColumn())
.toString();
// 如果旧的值 和新的值一样,直接 返回
if (nVal.equals(oldValue)) {
return;
}
// 判断当前编辑的单元格是否是主键列
}
Util.closeAll(conn, stmt, rt);
return true;
}
@Override
public boolean fundTicket(int userticketid, int userticketticketid) throws SQLException, Exception {
Connection con = Util.getConnection();
String sqlll = "update ticket set tickets_left=tickets_left+1 where ticketid = ?";
PreparedStatement stmttt = con.prepareStatement(sqlll);
stmttt.setInt(1, userticketticketid);
stmttt.executeUpdate();
String sql = "delete from userticket where userticketid=?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, userticketid);
stmt.executeUpdate();
return true;
}
@Override
public boolean SelectUidTid(int ticketid, int userid) throws SQLException {
boolean flag = true;
String sql = "select userticketuserid,userticketticketid from userticket";
Connection con = Util.getConnection();
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rr = stmt.executeQuery();
while (rr.next()) {
int useridd = rr.getInt(1);
int ticketidd = rr.getInt(2);
if (useridd == userid && ticketidd == ticketid) {
flag = false;
break;
}
}
Util.closeAll(con, stmt, rr);
return flag;
/*String sql = "select userticketuserid,userticketticketid";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, ticketid);
stmt.executeUpdate();
ResultSet rs = null;
DBcon.closeAll(stmt, rs);
return true;*/
int tickets_left = rs.getInt(9);
String fares = rs.getString(10);
String ticket_office = rs.getString(11);
Ticket ticket2 = new Ticket(ticketid0, station_arrival,
station_depart, depart_date, depart_time, ticket_number,
seattypeid, tickettypeid, tickets_left, fares, ticket_office);
ticket.add(ticket2);
}
Util.closeAll(con, stmt, rs);
return ticket;
}
@Override
public List<Ticket> quetida(String station_arrival, String station_depart, String depart_date) throws SQLException {
Connection con = Util.getConnection();
String sqladd = "select * from ticket where station_arrival=? and station_depart=? and depart_date=? ";
PreparedStatement stmt = con.prepareStatement(sqladd);
stmt.setString(1, station_arrival);
stmt.setString(2, station_depart);
stmt.setString(3, depart_date);
ResultSet rs = stmt.executeQuery();
List<Ticket> ticket = new ArrayList<>();
while (rs.next()) {
int ticketid0 = rs.getInt(1);
String station_arrival0 = rs.getString(2);
String station_depart0 = rs.getString(3);
String depart_date0 = rs.getString(4);
String depart_time = rs.getString(5);
int ticket_number = (rs.getInt(6));
String seattypeid = rs.getString(7);
String tickettypeid = rs.getString(8);
int tickets_left = rs.getInt(9);
String fares = rs.getString(10);
String ticket_office = rs.getString(11);
Ticket ticket2 = new Ticket(ticketid0, station_arrival0,
station_depart0, depart_date0, depart_time, ticket_number,
seattypeid, tickettypeid, tickets_left, fares, ticket_office);
ticket.add(ticket2);
}
System.out.println(rs.next());
Util.closeAll(con, stmt, rs);
return ticket;
station_depart, depart_date, depart_time, ticket_number,
seattypeid, tickettypeid, tickets_left, fares, ticket_office);
ticket.add(ticket2);
}
Util.closeAll(con, stmt, rs);
return ticket;
}
@Override
public List<Ticket> quetida(String station_arrival, String station_depart, String depart_date) throws SQLException {
Connection con = Util.getConnection();
String sqladd = "select * from ticket where station_arrival=? and station_depart=? and depart_date=? ";
PreparedStatement stmt = con.prepareStatement(sqladd);
stmt.setString(1, station_arrival);
stmt.setString(2, station_depart);
stmt.setString(3, depart_date);
ResultSet rs = stmt.executeQuery();
List<Ticket> ticket = new ArrayList<>();
while (rs.next()) {
int ticketid0 = rs.getInt(1);
String station_arrival0 = rs.getString(2);
String station_depart0 = rs.getString(3);
String depart_date0 = rs.getString(4);
String depart_time = rs.getString(5);
int ticket_number = (rs.getInt(6));
String seattypeid = rs.getString(7);
String tickettypeid = rs.getString(8);
int tickets_left = rs.getInt(9);
String fares = rs.getString(10);
String ticket_office = rs.getString(11);
Ticket ticket2 = new Ticket(ticketid0, station_arrival0,
station_depart0, depart_date0, depart_time, ticket_number,
seattypeid, tickettypeid, tickets_left, fares, ticket_office);
ticket.add(ticket2);
}
System.out.println(rs.next());
Util.closeAll(con, stmt, rs);
return ticket;
}
@Override
public int fares(int ticketid) throws SQLException {
Connection con = Util.getConnection();
int far = 0;
public void setPs(String ps) {
this.ps = ps;
}
public UserSelf() throws SQLException {
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);
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));//流式居中
refund = new JButton("修改我的信息");
panel.add(refund);
tuichu = new JButton("退出");
panel.add(tuichu);
tuichu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
Userlogin_db dg = new Userlogin_db();
int usd = dg.getUseridDl();
userdao = new UserDaoImpl();
List<User> us = userdao.QueryById(usd);
for (User e : us) {
int u = e.getUserid();
String name = e.getName();
String ident = e.getIdentity_number();
String ac = e.getAccount();
String pa = e.getPassword();
setId(u);
setIde(ident);
setPs(pa);
setAc(ac);
setNa(name);
setIde(ident);
private UserTicketDao usertickrtdao;
private UserDao userdao;
private TickeDao ticketdao;
JPanel contentPane;
JPanel contentPane4;
JPanel panelBottom;
public String sql;
Statement stmt;
public User_user_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);
try {
loadData();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
public void loadData() throws SQLException {
// 清除旧的数据
model.getDataVector().clear();
usertickrtdao = new UserTicketImpl();
Userlogin_db usd = new Userlogin_db();
int dfdf = usd.getUseridDl();
List<UserTicket> userticket = usertickrtdao.QueryByuserticketuserid(dfdf);
//List<Ticket> list = rs.queryAll();
// 遍历每一条数据,添加到model中
private TickeDao seatidDao;
private TicketImpl ticketimpl;
private TickettypeImpl tickettypeDao;
private SeattypeImpl seattypeDao;
private QueryUser query;
/**
* Create the panel.
*
* @param tickettypeDao
* @throws SQLException
*/
public AddTicket() throws SQLException {
setLayout(null);
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);
JLabel label1 = new JLabel("乘车站:");
label1.setBounds(280, 97, 146, 23);
add(label1);
txtstation_depart = new JTextField();
txtstation_depart.setBounds(350, 97, 146, 23);
add(txtstation_depart);
txtstation_arrival.setColumns(10);
JLabel label_20 = new JLabel("发车日期:");
label_20.setBounds(280, 130, 146, 23);
add(label_20);
txtdepart_date = new JTextField();
txtdepart_date.setBounds(350, 130, 146, 23);
add(txtdepart_date);
txtdepart_date.setColumns(10);
JLabel label_2 = new JLabel("发车时间:");
label_2.setBounds(280, 162, 146, 23);
add(label_2);
txtdepart_time = new JTextField();
txtdepart_time.setBounds(350, 162, 146, 23);
add(txtdepart_time);
txtdepart_time.setColumns(10);
JLabel label_3 = new JLabel("车次(整数):");
this.userticketseattype = userticketseattype;
this.usertickettickettype = usertickettickettype;
this.userticketfares = userticketfares;
this.userticketticket_office = userticketticket_office;
}
}
/**
* 个人信息修改
*/
public class UpdataSelf extends JFrame {
JPanel contentPane;
private JTextField txtname;
private JTextField txtidentity_number;
private JTextField txtaccout;
private JTextField txtPassword;
private UserDaoImpl userDao;
public UpdataSelf(int id, String name, String iden, String acc, String pass) {
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 100, 800, 800);
try {
if (flag) {
JOptionPane.showMessageDialog(null, "添加成功");
txtname.setText("");
txtidentity_number.setText("");
txtaccout.setText("");
txtPassword.setText("");
} else {
JOptionPane.showMessageDialog(null, "添加失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(320, 400, 116, 23);
add(btnNewButton);
}
}
/**
* 管理员操作界面
*/
String fares = rs.getString(10);
String ticket_office = rs.getString(11);
Ticket ticket2 = new Ticket(ticketid0, station_arrival0,
station_depart0, depart_date0, depart_time, ticket_number,
seattypeid, tickettypeid, tickets_left, fares, ticket_office);
ticket.add(ticket2);
}
System.out.println(rs.next());
Util.closeAll(con, stmt, rs);
return ticket;
}
@Override
public int fares(int ticketid) throws SQLException {
Connection con = Util.getConnection();
int far = 0;
String sqladd = "select fares from ticket where ticketid=? ";
PreparedStatement stmt = con.prepareStatement(sqladd);
stmt.setInt(1, ticketid);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
far = rs.getInt(1);
}
return far;
}
}
public class UserDaoImpl implements UserDao {
});
btnNewButton.setBounds(320, 480, 146, 23);
add(btnNewButton);
}
//public static void main(String[] args) {
//AddTicket s= new AddTicket();
//System.out.println(s.cmbtickettype.getSelectedItem());
// }
}
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));
String station_depart = txtstation_depart.getText();
String depart_date = txtdepart_date.getText();
String depart_time = txtdepart_time.getText();
String ticket_number0 = txtticket_number.getText();
int ticket_number = Integer.parseInt(ticket_number0);
Seattype seattype = (Seattype) cmbseattype.getSelectedItem();
String seattypeid = String.valueOf(seattype.getSeattypename()).toString();
Tickettype tickettype = (Tickettype) cmbtickettype.getSelectedItem();
String tickettypeid = String.valueOf(tickettype);
String tickets_left0 = txttickets_left.getText();
int tickets_left = Integer.parseInt(tickets_left0);
String fares = txtfares.getText();
String ticket_office = txtticket_office.getText();
Ticket ticket = new Ticket(ticketid, station_arrival, station_depart,
depart_date, depart_time, ticket_number, seattypeid,
tickettypeid, tickets_left, fares, ticket_office);
boolean flag;
try {
flag = ticketDao.update(ticket);
if (flag) {
JOptionPane.showMessageDialog(null, "修改成功");
dispose();
} else {
JOptionPane.showMessageDialog(null, "修改失败");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNewButton.setBounds(320, 480, 146, 23);
add(btnNewButton);
}
}
/**
* 添加车票
*/
@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=?";
model.addRow(new Object[]{
b.getUserticketid(), b.getUserticketticketid(), b.getUserticketuserid(), b.getUserticketname(), b.getUserticketidentity_number(),
b.getUserticketstation_arrival(), b.getUserticketstation_depart(), b.getUserticketdepart_date(),
b.getUserticketdepart_time(), b.getUserticketidentity_number(), b.getUserticketseattype(),
b.getUsertickettickettype(), b.getUserticketfares(), b.getUserticketticket_office()
});
}
}
/**
* 退票
*
* @throws Exception
*/
public void refund() throws Exception {
if (table.getSelectedRowCount() <= 0) {
JOptionPane.showMessageDialog(null, "请选择要退的票");
return;
}
int result = JOptionPane.showConfirmDialog(null, "是否确定退掉该票");
// 判断用户是否点击
if (result == JOptionPane.OK_OPTION) {
int userticketid = Integer.valueOf(table.getValueAt(
table.getSelectedRow(), 0).toString());
int userticketuserid = Integer.valueOf(table.getValueAt(
table.getSelectedRow(), 2).toString());
int userticketticketid = Integer.valueOf(table.getValueAt(
table.getSelectedRow(), 1).toString());
usertickrtdao = new UserTicketImpl();
userdao = new UserDaoImpl();
ticketdao = new TicketImpl();
usertickrtdao.fundTicket(userticketid, userticketticketid);
int yuan = ticketdao.fares(userticketticketid);
System.out.println("票价" + yuan);
userdao.addBlance(userticketuserid, yuan);
try {
loadData();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
int result = JOptionPane.showConfirmDialog(null, "是否确定要修改");
// 判断用户是否点击
if (result == JOptionPane.OK_OPTION) {
int userid = Integer.valueOf(table.getValueAt(table.getSelectedRow(), 0).toString());
String name = table.getValueAt(table.getSelectedRow(), 1).toString();
String identity_number = table.getValueAt(table.getSelectedRow(), 2).toString();
String account = table.getValueAt(table.getSelectedRow(), 3).toString();
String password = table.getValueAt(table.getSelectedRow(), 4).toString();
User user = new User(userid, name, identity_number, account, password);
UpdataUser upd = new UpdataUser(user);
upd.setVisible(true);
}
}
public void chongzhi() {
if (table.getSelectedRowCount() <= 0) {
JOptionPane.showMessageDialog(null, "请选择要修改的数据行");
return;
}
int result = JOptionPane.showConfirmDialog(null, "是否确定要充值");
// 判断用户是否点击
if (result == JOptionPane.OK_OPTION) {
int userid = Integer.valueOf(table.getValueAt(table.getSelectedRow(), 0).toString());
top_up tu = new top_up(userid);
tu.setVisible(true);
}
}
}
/**
* 用户信息修改
*/
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;
}
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("");