资源描述
火车售票系统完整代码
精品文档
package com.yue;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class BuyPanel extends JPanel {
StringBuffer sb = new StringBuffer();
private boolean flag = false;
JTextArea jtArea = new JTextArea(10, 78);
JLabel buyLabel = new JLabel("订票");
JTextField fId = new JTextField(7);
JTextField ftid = new JTextField(7);
JTextField fche = new JTextField(7);
JTextField fzhangshu = new JTextField(7);
JTextField fbodyId = new JTextField(7);
JPasswordField jpField = new JPasswordField(7);
public JPanel getBuypn() {
JPanel pan = new JPanel();
GridBagLayout layout = new GridBagLayout();
pan.setLayout(layout);
GridBagConstraints con = getGridBagConstraints(0, 0, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.WEST);
pan.add(buyLabel, con);
con = getGridBagConstraints(0, 2, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.WEST);
pan.add(new pan3(), con);
con = getGridBagConstraints(0, 3, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.WEST);
pan.add(new pan4(), con);
con = getGridBagConstraints(0, 4, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
pan.add(new pan5(), con);
this.setLocation(270, 150);
this.add(pan);
return pan;
}
/**
* 定义下面购票操作区面板pan3
*
* @author Administrator
*
*/
class pan3 extends JPanel {
JLabel lId = new JLabel(" 车票号码 ");
JLabel ltid = new JLabel(" 车票号码 ");
JLabel che = new JLabel(" 车 次 ");
JLabel lbodyId = new JLabel(" 身份证号 ");
JLabel password = new JLabel(" 密码 ");
JLabel lzhangshu = new JLabel(" 所需张数 ");
JLabel la1 = new JLabel(" ");
JLabel la2 = new JLabel(" ");
JButton sureButton = new JButton("查询确认");
JButton buyButton = new JButton("确认购票");
public pan3() {
buyButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
UpdateUser();
if (flag==true){
UpdateTrain();
System.out.println("hjkasfhjkaf");
}
}
});
sureButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
select();
}
});
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
GridBagConstraints con = getGridBagConstraints(0, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(lId, con);
con = getGridBagConstraints(1, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(fId, con);
con = getGridBagConstraints(8, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(la2, con);
con = getGridBagConstraints(9, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(sureButton, con);
con = getGridBagConstraints(0, 1, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(ltid, con);
con = getGridBagConstraints(1, 1, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(ftid, con);
con = getGridBagConstraints(2, 1, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(che, con);
con = getGridBagConstraints(3, 1, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(fche, con);
con = getGridBagConstraints(4, 1, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(lbodyId, con);
con = getGridBagConstraints(5, 1, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(fbodyId, con);
con = getGridBagConstraints(0, 2, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(password, con);
con = getGridBagConstraints(1, 2, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(jpField, con);
con = getGridBagConstraints(2, 2, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(lzhangshu, con);
con = getGridBagConstraints(3, 2, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(fzhangshu, con);
con = getGridBagConstraints(8, 2, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(la1, con);
con = getGridBagConstraints(9, 2, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(buyButton, con);
this.setBorder(BorderFactory.createTitledBorder("订票操作区"));
}
}
/**
* 定义下面一个JList用于显示车票的所有信息面板pan4
*
* @author Administrator
*
*/
class pan4 extends JPanel {
JButton btId = new JButton("车票号码");
JButton bt1 = new JButton("起 点 站");
JButton bt2 = new JButton("终 点 站");
JButton bt3 = new JButton("车 次");
JButton bt4 = new JButton("发车时间");
JButton bt5 = new JButton("到达时间");
JButton bt6 = new JButton("座位类型");
JButton bt7 = new JButton("发车日期");
JButton bt8 = new JButton("剩余票数");
JButton bt9 = new JButton("车票价格");
public pan4() {
this.setLayout(new GridBagLayout());
GridBagConstraints con = getGridBagConstraints(0, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(btId, con);
con = getGridBagConstraints(1, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt1, con);
con = getGridBagConstraints(2, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt2, con);
con = getGridBagConstraints(3, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt3, con);
con = getGridBagConstraints(4, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt4, con);
con = getGridBagConstraints(5, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt5, con);
con = getGridBagConstraints(6, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt6, con);
con = getGridBagConstraints(7, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt7, con);
con = getGridBagConstraints(8, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt8, con);
con = getGridBagConstraints(9, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt9, con);
con = getGridBagConstraints(0, 1, 11, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(jtArea, con);
jtArea.setBackground(Color.GREEN);
}
}
/**
* 定义最下面的按钮面板pan5
*
* @author Administrator
*
*/
class pan5 extends JPanel {
JButton bt1 = new JButton("退出登录");
JLabel lab1 = new JLabel(" ");
public pan5() {
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
GridBagConstraints con = getGridBagConstraints(0, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(bt1, con);
con = getGridBagConstraints(1, 0, 1, 1, 0, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
this.add(lab1, con);
bt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
/**
* 放回一个网格包约束
*
* @param x
* @param y
* @param width
* @param heigh
* @param weighx
* @param weighy
* @param fill
* @param anchor
* @return
*/
private GridBagConstraints getGridBagConstraints(int x, int y, int width,
int heigh, int weighx, int weighy, int fill, int anchor) {
GridBagConstraints con = new GridBagConstraints();
con.gridx = x;
con.gridy = y;
con.gridwidth = width;
con.gridheight = heigh;
con.weightx = weighx;
con.weighty = weighy;
con.fill = fill;
con.anchor = anchor;
return con;
}
/**
* 按车票号码搜索所有的车票信息
*
* @throws SQLException
*/
private void select() {
Connection conn = null;
Statement stmt = null;
ResultSet ticket = null;
try {
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "store", "store");
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
stmt = conn.createStatement();
String text = "select * from train_table " + "where tid=" + "'"
+ fId.getText() + "'";
ticket = stmt.executeQuery(text);
while (ticket.next()) {
String starter = ticket.getString("starter");
String reacher = ticket.getString("reacher");
String ticketNumber = ticket.getString("ticketNumber");
String startTime = ticket.getString("startTime");
String endTime = ticket.getString("endTime");
String seatIdNumber = ticket.getString("seatIdNumber");
String busDate = ticket.getString("busDate");
int ticketNum = ticket.getInt("ticketNum");
double price = ticket.getDouble("price");
sb.append(" " + fId.getText() + "\t"+ " " + starter + "\t"+ " " + reacher
+ "\t"+ " " + ticketNumber + "\t" + startTime + "\t"
+ endTime + "\t" + seatIdNumber + "\t" + busDate + "\t"
+ ticketNum + "\t" + price + " " + "\n");
}
jtArea.setText(sb.toString());
if (sb.length() > 0) {
sb.delete(1, sb.length());
} else {
JOptionPane.showMessageDialog(new TestFrame(), "无此车!");
}
} catch (SQLException e) {
} finally {
if (ticket != null) {
try {
ticket.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 订票时更新数据库中的车票表
*/
private void UpdateTrain() {
String tId=ftid.getText();
int num=0;
if (fzhangshu.getText().length()>0){
num=Integer.parseInt(fzhangshu.getText());
}
Connection conn = Getgbc.getconnect();
CallableStatement cst = null;
try {
conn.setAutoCommit(false);
cst = conn.prepareCall("call train_proc (?,?)");
cst.setString(1, tId);
cst.setInt(2, num);
cst.execute();
mit();
} catch (SQLException e1) {
try {
conn.rollback();
JOptionPane.showMessageDialog(new TestFrame(), "客户订票失败!");
} catch (SQLException e2) {
JOptionPane.showMessageDialog(new TestFrame(), "客户订票失败!");
}
} finally {
if (cst != null) {
try {
cst.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 订票时更新数据库中的用户表
*/
private void UpdateUser() {
String tId=ftid.getText();
String bId=fbodyId.getText();
String cID=fche.getText();
String pwd=String.valueOf(jpField.getPassword());
int num=0;
if((bId.equals(EnterFrame.ID))&&(pwd.equals(EnterFrame.password))){
if(fzhangshu.getText().length()>0)
{
num=Integer.parseInt(fzhangshu.getText());
}
Connection conn = Getgbc.getconnect();
Statement cst = null;
try {
cst = conn.createStatement();
cst.executeUpdate("insert into order_table(tid,id_number,t_num,trainsum)"
+ " values('"
+ tId
+ "','"
+ bId
+ "','"
+ cID + "','"+num+"')");
JOptionPane.showMessageDialog(new TestFrame(), "客户订票成功!");
flag=true;
} catch (SQLException e1) {
JOptionPane.showMessageDialog(new TestFrame(), "订票失败!");
flag=false;
} finally {
if (cst != null) {
try {
cst.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
else{
JOptionPane.showMessageDialog(new TestFrame(), "您的身份证或密码有误!请查证后再订票");
}
}
}
package com.yue;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CheckPanel extends JPanel {
StringBuffer sb = new StringBuffer();
JTextField trainField = new JTextField(9);
JTextField dateField = new JTextField(9);
JTextField stationField = new JTextField(9);
JTextField zhiField = new JTextField(9);
JTextArea jtArea = new JTextArea(10, 80);
JLabel checkLabel = new JLabel("查询");
public JPanel getCheckpn() {
JPanel pan = new JPanel();
GridBagLayout layout = new GridBagLayout();
pan.setLayout(layout);
GridBagConstraints con = getGridBagConstraints(0, 0, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.WEST);
pan.add(checkLabel, con);
con = getGridBagConstraints(0, 1, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.WEST);
pan.add(new pan2(), con);
con = getGridBagConstraints(0, 3, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.WEST);
pan.add(new pan4(), con);
con = getGridBagConstraints(0, 4, 1, 1, 100, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
pan.add(new pan5(), con);
this.setLocation(270, 150);
this.add(pan);
return pan;
}
/**
* 定义上部查询区面pan2
*
* @author Administrator
*
*/
class pan2 extends JPanel {
JLabel trainLabel = new JLabel(" 车 次 ");
JLabel dateLabel = new JLabel(" 日 期 ");
JLabel stationLabel = new JLabel(" 起点站 ");
JLabel zhiLabel = new JLabel(" 终点站 ");
JLabel l1 = new JLabel(" ");
JLabel l2 = new JLabel(" ");
JButton checkButton = new JButton("查
展开阅读全文