赞
踩
在Java——Demo使用GUI并连接数据库实现购买火车票<MySQL>基础上
修改了车次查找
取消了按车次名查找
增添了根据起点站→和终点站进行查询
修改了点击购买按钮弹出下单页面
【tickets数据表】添加id列,设置主键和自动增长,后续用来根据行和id查询车次
既然【tickets数据表】改了,那么【tickets.java类】也要添加id属性
主窗口去除购买按钮
查找输入文本框修改为起始站输入框和终点站文本框
查找按钮的点击事件改为
// 查找按钮 button_search.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { Connection con = null; try { con = source.getConnection(); String start = searchStartText.getText(); String end = searchEndText.getText(); String sql = "SELECT * FROM tickets WHERE START=? AND END=?"; loadticket(textArea_1, sql, start, end); } catch (SQLException e) { e.printStackTrace(); } finally { if (con != null) DbUtils.closeQuietly(con); } } });
loadticket()
方法:
// 打印tickets数据表中的所有车票信息 public void loadticket(JTextArea textArea, String sql, Object... args) { Connection con = null; try { // 获取连接 con = source.getConnection(); BeanListHandler<Tickets> ticketbean = new BeanListHandler<Tickets>(Tickets.class); List<Tickets> ticketlist = null; if (args.length == 0) { ticketlist = runner.query(con, sql, ticketbean); } else { ticketlist = runner.query(con, sql, ticketbean, args[0], args[1]); } if (ticketlist.size() == 0 && args.length > 0) { textArea.setText(ticketload + "未找到该车次"); } else { Iterator<Tickets> iter = ticketlist.iterator(); String ticstr = ""; while (iter.hasNext()) { Tickets ticket = iter.next(); ticstr += " " + ticket.getName() + "\t" + ticket.getStart() + "\t" + ticket.getEnd() + "\t" + ticket.getPrice() + "\t" + ticket.getNumber() + "\n"; } textArea.setText(ticketload + ticstr); } } catch (SQLException e1) { e1.printStackTrace(); } finally { // 释放连接 DbUtils.closeQuietly(con); } }
如果用id查找会有些不足,因为id是自动增长,删除之后也不会从1开始,各位勇士可以想想办法解决,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。