火车售票系统
1. 登陆
1.1登录界面显示
1.2 登陆功能实现
/* * 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 view;
import dao.connectDao;
import dao.StringUil;
import dao.UserDao;
import 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登陆功能数据库实现
package dao;
import model.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDao {
public User login(Connection con,User user) throws Exception{
User resultUser=null;
String sql="select * from t_user where username=? and password=?";
PreparedStatement pstme = con.prepareStatement(sql);
pstme.setString(1,user.getName());
pstme.setString(2,user.getPassword());
ResultSet rs=pstme.executeQuery();
if(rs.next()){
resultUser=new User();
resultUser.setId(rs.getInt("id"));
resultUser.setName(rs.getString("username"));
resultUser.setName(rs.getString("password"));
}
return resultUser;
}
public int register(Connection con,User user) throws Exception{
String sql="insert into t_user values(null,?,?,?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getQQ());
pstmt.setString(4,user.getIphone());
return pstmt.executeUpdate();
}
}
2. 站点查询
2.1站点查询界面显示
2.2 站点查询功能实现
/* * 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 view;
import dao.connectDao;
import dao.StringUil;
import dao.TricketTypeDao;
import model.TricketType;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
public class typeSelectFrame extends javax.swing.JInternalFrame {
private connectDao conn=new connectDao();
private TricketTypeDao tricketTypeDao=new TricketTypeDao();
public typeSelectFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
station_inframe = new JScrollPane();
stationTypeTable = new JTable();
jLabel1 = new JLabel();
stationName = new JTextField();
selectButton = new JButton();
jPanel1 = new JPanel();
jLabel2 = new JLabel();
idTxt = new JTextField();
jLabel3 = new JLabel();
stationTxt = new JTextField();
jLabel4 = new JLabel();
jScrollPane1 = new JScrollPane();
describeTxt = new JTextArea();
updateButton = new JButton();
deleteButton = new JButton();
setClosable(true);
setIconifiable(true);
setTitle("火车站点查询");
setPreferredSize(new java.awt.Dimension(523, 600));
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
resetValue();
}
});
stationTypeTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
stationTypeTableMousePressed(e);
}
});
updateButton.addActionListener(this::updateButtonActionPerformed);
stationTypeTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{
null, null, null},
{
null, null, null},
{
null, null, null},
{
null, null, null}
},
new String [] {
"车站id", "站点名称", "站点信息"
}
));
station_inframe.setViewportView(stationTypeTable);
jLabel1.setText("站点名称:");
selectButton.setText("查询");
selectButton.addActionListener(this::selectButtonActionPerformed);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("表单操作"));
jLabel2.setText("编号:");
idTxt.setEditable(false);
jLabel3.setText("站点名称:");
jLabel4.setText("描述:");
describeTxt.setColumns(20);
describeTxt.setRows(5);
jScrollPane1.setViewportView(describeTxt);
updateButton.setText("更新");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
deleteButton.setText("删除");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap(34, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(idTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(stationTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1))
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(61, 61, 61)
.addComponent(updateButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(deleteButton)
.addGap(50, 50, 50))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(15, 15, 15)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(idTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(stationTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(updateButton)
.addComponent(deleteButton))
.addContainerGap())
);
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(70, 70, 70)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(stationName, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
.addComponent(selectButton))
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(station_inframe, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(55, 55, 55))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(stationName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(selectButton))
.addGap(32, 32, 32)
.addComponent(station_inframe, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(26, Short.MAX_VALUE))
);
pack();
}
private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {
String id=idTxt.getText();
String station =stationTxt.getText();
String describe=describeTxt.getText();
if(StringUil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择需要修改的数据");
return;
}
System.out.println(Integer.parseInt(id)+" "+station+ " "+ describe);
TricketType trick=new TricketType(Integer.parseInt(id),station,describe);
System.out.println(trick.getId()+" "+trick.getStation()+" "+trick.getDescribe());
Connection con=null;
try {
con=conn.getCon();
int number=tricketTypeDao.update(con,trick);
if(number==1) {
JOptionPane.showMessageDialog(null,"修改成功");
resetValue();
this.fillTable(new TricketType());
}else{
JOptionPane.showMessageDialog(null,"修改失败1");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"修改失败2");
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void resetValue(){
this.idTxt.setText("");
this.describeTxt.setText("");
this.stationTxt.setText("");
}
private void stationTypeTableMousePressed(MouseEvent e){
int row = stationTypeTable.getSelectedRow();
idTxt.setText((String) stationTypeTable.getValueAt(row,0));
stationTxt.setText((String)stationTypeTable.getValueAt(row,1));
describeTxt.setText((String)stationTypeTable.getValueAt(row,2));
}
private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {
String s_stationName=this.stationName.getText();
TricketType tricket_type=new TricketType();
tricket_type.setStation(s_stationName);
this.fillTable(tricket_type);
}
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
String id =idTxt.getText();
if(StringUil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择需要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null,"确定要删除这条记录吗?");
if(n==0){
Connection con=null;
try {
con=conn.getCon();
int deleteNum=tricketTypeDao.delete(con,id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null,"删除成功");
this.resetValue();
this.fillTable(new TricketType());
}else{
JOptionPane.showMessageDialog(null,"删除失败");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"删除失败");
e.printStackTrace();
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private javax.swing.JButton deleteButton;
private javax.swing.JTextArea describeTxt;
private javax.swing.JTextField idTxt;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField stationName;
private javax.swing.JButton selectButton;
private javax.swing.JTextField stationTxt;
private javax.swing.JTable stationTypeTable;
private javax.swing.JScrollPane station_inframe;
private javax.swing.JButton updateButton;
private void fillTable(TricketType tricket_type){
DefaultTableModel dtm =(DefaultTableModel) stationTypeTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=conn.getCon();
ResultSet rs=tricketTypeDao.list(con,tricket_type);
while(rs.next()){
Vector v=new Vector();
v.add(rs.getString("id"));
v.add(rs.getString("station"));
v.add(rs.getString("describee"));
dtm.addRow(v);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2.3 站点查询功能数据库实现
package dao;
import 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();
}
}
3. 售票类型增加
3.1 售票类型界面显示
3.2 售票类型功能界面实现
package view;
import dao.StringUil;
import dao.TricketDao;
import dao.TricketTypeDao;
import dao.connectDao;
import model.Tricket;
import model.TricketType;
import javax.swing.*;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.text.ParseException;
public class tricketAddInterFrame extends javax.swing.JInternalFrame {
public tricketAddInterFrame() {
initComponents();
}
private JComboBox<TricketType> endStationBox;
private javax.swing.JComboBox<String> seatBox;
private JComboBox<TricketType> startStationBox;
private connectDao conn=new connectDao();
private TricketTypeDao tricketTypeDao=new TricketTypeDao();
private TricketDao tricketdao=new TricketDao();
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
TricketIDTxt = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
startTimetxt = new javax.swing.JTextField();
endTimeTxt = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
remarkTxt = new javax.swing.JTextArea();
jLabel8 = new javax.swing.JLabel();
startStationBox = new javax.swing.JComboBox<TricketType>();
endStationBox = new JComboBox<TricketType>();
seatBox = new javax.swing.JComboBox<>();
jLabel9 = new javax.swing.JLabel();
priceTxt = new javax.swing.JTextField();
insertButton = new javax.swing.JButton();
receiveButton = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setTitle("车票添加");
jLabel1.setText("车次编号:");
jLabel2.setText("出发站点:");
jLabel3.setText("到达站点:");
jLabel4.setText("座位信息:");
jLabel5.setText("发车时间:");
jLabel6.setText("到达时间:");
jLabel7.setText("备 注:");
remarkTxt.setColumns(20);
remarkTxt.setRows(5);
jScrollPane1.setViewportView(remarkTxt);
jLabel8.setFont(new java.awt.Font("华文行楷", 1, 24)); // NOI18N
jLabel8.setText("车票信息填写");
startStationBox.setModel(new javax.swing.DefaultComboBoxModel<TricketType>(new TricketType[] {
}));
startStationBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startStationBoxActionPerformed(evt);
}
});
endStationBox.setModel(new javax.swing.DefaultComboBoxModel<TricketType>(new TricketType[] {
}));
endStationBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
endStationBoxActionPerformed(evt);
}
});
seatBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] {
"硬座", "软座", "硬卧", "软卧" }));
seatBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
seatActionPerformed(evt);
}
});
jLabel9.setText("票 价:");
insertButton.setText("添加");
insertButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
insertButtonActionPerformed(evt);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
receiveButton.setText("重置");
receiveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
receiveButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(146, 146, 146)
.addComponent(jLabel8))
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(TricketIDTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(startStationBox, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3))
.addGroup(layout.createSequentialGroup()
.addComponent(seatBox, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel9)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(priceTxt)
.addComponent(endStationBox, 0, 90, Short.MAX_VALUE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(startTimetxt))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(endTimeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel7)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(insertButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(receiveButton))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addContainerGap(49, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jLabel8)
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(TricketIDTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(startStationBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(endStationBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(seatBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9)
.addComponent(priceTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(startTimetxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(endTimeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(insertButton)
.addComponent(receiveButton))
.addContainerGap(70, Short.MAX_VALUE))
);
pack();
fillstartStationBox();
}
private void fillstartStationBox(){
Connection con=null;
TricketType tricketType=new TricketType();
try {
con=conn.getCon();
ResultSet rs =TricketTypeDao.list(con,new TricketType());
while(rs.next()){
tricketType=new TricketType();
tricketType.setId(rs.getInt("id"));
tricketType.setStation(rs.getString("station"));
this.startStationBox.addItem(tricketType);
this.endStationBox.addItem(tricketType);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void startStationBoxActionPerformed(java.awt.event.ActionEvent evt) {
}
private void endStationBoxActionPerformed(java.awt.event.ActionEvent evt) {
}
private void seatActionPerformed(java.awt.event.ActionEvent evt) {
}
private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) throws ParseException {
String trickID = TricketIDTxt.getText();
String price = priceTxt.getText();
String remarks = remarkTxt.getText();
if (StringUil.isEmpty(trickID)) {
JOptionPane.showMessageDialog(null,"车次编号不能为空!!!");
return;
}else if(StringUil.isEmpty(price)) {
JOptionPane.showMessageDialog(null,"车票价格不能为空!!!");
return;
}else if(StringUil.isEmpty(startTimetxt.getText())) {
JOptionPane.showMessageDialog(null,"发车时间不能为空!!!");
return;
}else if(StringUil.isEmpty(endTimeTxt.getText())) {
JOptionPane.showMessageDialog(null,"到站时间不能为空!!!");
return;
}
java.sql.Date endTime = Date.valueOf(endTimeTxt.getText());
java.sql.Date startTime = Date.valueOf(startTimetxt.getText());
String seatt=(String)seatBox.getSelectedItem();
TricketType tricketType=(TricketType) startStationBox.getSelectedItem();
TricketType tricketType1=(TricketType) endStationBox.getSelectedItem();
assert tricketType != null;
assert tricketType1 != null;
if(tricketType.getId()==tricketType1.getId()){
JOptionPane.showMessageDialog(null,"出发地与目的地相同,请重新选择!!");
return;
}
Tricket tricket=new Tricket(Integer.parseInt(trickID),tricketType.getId(),tricketType.getStation(),tricketType1.getId(),
tricketType1.getStation(),seatt,Float.parseFloat(price),startTime,
endTime,remarks);
Connection con=null;
try {
con=conn.getCon();
int addNum=TricketDao.add(con,tricket);
if(addNum==1){
JOptionPane.showMessageDialog(null,"车票添加成功");
resetValue();
}else{
JOptionPane.showMessageDialog(null,"车票添加失败!!");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"车票添加失败!!");
e.printStackTrace();
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"数据库关闭失败!!");
e.printStackTrace();
}
}
}
private void resetValue(){
this.remarkTxt.setText("");
this.endTimeTxt.setText("");
this.priceTxt.setText("");
this.TricketIDTxt.setText("");
this.startTimetxt.setText("");
if(this.endStationBox.getItemCount()>0){
this.endStationBox.setSelectedIndex(0);
}
if(this.startStationBox.getItemCount()>0){
this.startStationBox.setSelectedIndex(0);
}
if(this.seatBox.getItemCount()>0){
this.seatBox.setSelectedIndex(0);
}
}
private void receiveButtonActionPerformed(java.awt.event.ActionEvent evt) {
resetValue();
}
private javax.swing.JTextField TricketIDTxt;
private javax.swing.JTextField endTimeTxt;
private javax.swing.JButton insertButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea remarkTxt;
private javax.swing.JTextField priceTxt;
private javax.swing.JButton receiveButton;
private javax.swing.JTextField startTimetxt;
// End of variables declaration
}
3.3 售票类型功能数据库实现
package dao;
import model.TricketType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class TricketTypeDao {
public int add(Connection con, TricketType tricket_type) throws Exception{
String sql="insert into station_inform values(null,?,?)";
PreparedStatement pstmt =con.prepareStatement(sql);
System.out.println(tricket_type.getStation()+" "+tricket_type.getDescribe());
pstmt.setString(1,tricket_type.getStation());
pstmt.setString(2,tricket_type.getDescribe());
return pstmt.executeUpdate();
}
public static ResultSet list(Connection con, TricketType tricket_type) throws Exception {
StringBuffer strb =new StringBuffer("select * from station_inform");
if(StringUil.isNotEmpty(tricket_type.getStation())){
strb.append(" and station like '%"+tricket_type.getStation()+"%'");
}
PreparedStatement pstm=con.prepareStatement(strb.toString().replaceFirst("and","where"));
return pstm.executeQuery();
}
public int delete(Connection con,String id) throws Exception{
String sql="delete from station_inform where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,id);
return pstmt.executeUpdate();
}
public int update(Connection con, TricketType tricket)throws Exception{
String sql="Update station_inform set station=?,describee=? where id=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,tricket.getStation());
pstmt.setString(2,tricket.getDescribe());
pstmt.setInt(3,tricket.getId());
return pstmt.executeUpdate();
}
}
4. 售票管理
4.1售票管理界面显示
4.2 售票管理功能实现
package view;
import dao.StringUil;
import dao.TricketDao;
import dao.TricketTypeDao;
import dao.connectDao;
import model.Tricket;
import model.TricketType;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.Vector;
public class tricketSelectFrame extends javax.swing.JInternalFrame {
private connectDao conn=new connectDao();
private TricketTypeDao tricketTydao=new TricketTypeDao();
private TricketDao tricketdao=new TricketDao();
public tricketSelectFrame() {
initComponents();
}
private JComboBox<TricketType> s_endStationBox;
private JComboBox<TricketType> s_startStationBox;
private javax.swing.JTextField s_tricketID;
private javax.swing.JTable resultTable;
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
resultTable = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
s_tricketID = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
selectButton = new javax.swing.JButton();
s_startStationBox = new JComboBox<TricketType>();
s_endStationBox = new javax.swing.JComboBox<TricketType>();
jPanel2 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
tricketIdTxt = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
startStationTxt = new javax.swing.JTextField();
jLabel6 = new javax.swing.JLabel();
endStationTxt = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
seatTypeTxt = new javax.swing.JTextField();
jLabel8 = new javax.swing.JLabel();
priceTxt = new javax.swing.JTextField();
jLabel9 = new javax.swing.JLabel();
startTimeTxt = new javax.swing.JTextField();
jLabel10 = new javax.swing.JLabel();
endTimeTxt = new javax.swing.JTextField();
jLabel11 = new javax.swing.JLabel();
remarksTxt = new javax.swing.JTextField();
updateButton = new javax.swing.JButton();
deleteButton = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setTitle("车票管理");
resultTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{
null, null, null, null, null, null, null, null},
{
null, null, null, null, null, null, null, null},
{
null, null, null, null, null, null, null, null},
{
null, null, null, null, null, null, null, null},
{
null, null, null, null, null, null, null, null},
{
null, null, null, null, null, null, null, null},
{
null, null, null, null, null, null, null, null}
},
new String [] {
"车次编号", "出发站名", "目的站名", "座位类型", "车票价格", "发车时间", "到达时间", "备注"
}
) {
Class[] types = new Class [] {
java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
resultTable.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
resultTableMousePressed(evt);
}
});
jScrollPane1.setViewportView(resultTable);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("搜索条件"));
jLabel1.setText("车次信息:");
jLabel2.setText("出发站点:");
jLabel3.setText("目的站点:");
selectButton.setText("搜索");
selectButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
selectButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(s_tricketID, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(s_startStationBox, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(s_endStationBox, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(selectButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(s_tricketID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jLabel1)
.addComponent(jLabel3)
.addComponent(selectButton)
.addComponent(s_startStationBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(s_endStationBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(22, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("车票修改"));
jLabel4.setText("车次编号:");
tricketIdTxt.setEditable(false);
startStationTxt.setEditable(false);
endStationTxt.setEditable(false);
jLabel5.setText("出发站点:");
jLabel6.setText("目的站点:");
jLabel7.setText("座位类型:");
jLabel8.setText("车票价格:");
jLabel9.setText("发车时间:");
startTimeTxt.setEditable(true);
jLabel10.setText("到达时间:");
endTimeTxt.setEditable(true);
jLabel11.setText("备注:");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel7)
.addGap(18, 18, 18)
.addComponent(seatTypeTxt))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(tricketIdTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel10)
.addGap(18, 18, 18)
.addComponent(endTimeTxt))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(startStationTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel8)
.addGap(18, 18, 18)
.addComponent(priceTxt)))
.addGap(18, 18, 18)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(endStationTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel11)
.addComponent(jLabel9))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(startTimeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(434, 434, 434))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(remarksTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 336, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(tricketIdTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(startStationTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)
.addComponent(endStationTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(seatTypeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8)
.addComponent(priceTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(startTimeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel10)
.addComponent(endTimeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel11)
.addComponent(remarksTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(25, Short.MAX_VALUE))
);
updateButton.setText("修改");
updateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateButtonActionPerformed(evt);
}
});
deleteButton.setText("删除");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(updateButton)
.addGap(45, 45, 45)
.addComponent(deleteButton))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap(45, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(27, 27, 27)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(43, 43, 43)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(updateButton)
.addComponent(deleteButton))
.addContainerGap(31, Short.MAX_VALUE))
);
this.fillTricketBox();
//this.fillTable(new Tricket());
pack();
}// </editor-fold>
private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String tricketID=this.s_tricketID.getText();
TricketType tricketType=(TricketType) this.s_startStationBox.getSelectedItem();
TricketType tricketType1=(TricketType) this.s_endStationBox.getSelectedItem();
String start=tricketType.getStation();
String end=tricketType1.getStation();
Tricket tricket=null;
if(StringUil.isEmpty(tricketID)){
tricket=new Tricket(-1,start,end);
}else{
tricket=new Tricket(Integer.parseInt(tricketID),start,end);
}
this.fillTable(tricket);
}
private void resultTableMousePressed(MouseEvent evt) {
// TODO add your handling code here:
int row= this.resultTable.getSelectedRow();
this.tricketIdTxt.setText((String) resultTable.getValueAt(row,0));
this.startStationTxt.setText((String) resultTable.getValueAt(row,1));
this.endStationTxt.setText((String) resultTable.getValueAt(row,2));
this.seatTypeTxt.setText((String) resultTable.getValueAt(row,3));
this.priceTxt.setText( resultTable.getValueAt(row,4)+"");
this.startTimeTxt.setText( resultTable.getValueAt(row,5)+"");
this.endTimeTxt.setText( resultTable.getValueAt(row,6)+"");
this.remarksTxt.setText((String) resultTable.getValueAt(row,7));
}
private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String id =this.tricketIdTxt.getText();
if(StringUil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择需要修改的数据");
return;
}
int ID=Integer.parseInt(id);
String start=this.startStationTxt.getText();
String end=this.endStationTxt.getText();
String seat=this.seatTypeTxt.getText();
Float price=Float.parseFloat(this.priceTxt.getText());
java.sql.Date starttime=java.sql.Date.valueOf(this.startTimeTxt.getText());
Date endtime=java.sql.Date.valueOf(this.endTimeTxt.getText());
String remarks=this.remarksTxt.getText();
Tricket tricket=new Tricket(ID,start,end,price,seat,starttime,endtime,remarks);
Connection con=null;
try {
con=conn.getCon();
int addNum=TricketDao.update(con,tricket);
if(addNum==1){
JOptionPane.showMessageDialog(null,"车票修改成功");
this.fillTable(new Tricket());
resetValue();
}else{
JOptionPane.showMessageDialog(null,"车票修改失败!!");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"车票修改失败!!");
e.printStackTrace();
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"数据库关闭失败!!");
e.printStackTrace();
}
}
}
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String id =this.tricketIdTxt.getText();
if(StringUil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择需要删除的记录");
return;
}
int n=JOptionPane.showConfirmDialog(null,"确定要删除这条记录吗?");
if(n==0){
Connection con=null;
try {
con=conn.getCon();
int deleteNum=tricketdao.delete(con,id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null,"删除成功");
this.resetValue();
this.fillTable(new Tricket(
));
}else{
JOptionPane.showMessageDialog(null,"删除失败");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"删除失败");
e.printStackTrace();
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void fillTricketBox(){
Connection con=null;
TricketType tricketType=null;
try {
con=conn.getCon();
ResultSet rs= TricketTypeDao.list(con,new TricketType());
tricketType=new TricketType();
tricketType.setStation("请选择...");
tricketType.setId(-1);
this.s_endStationBox.addItem(tricketType);
this.s_startStationBox.addItem(tricketType);
while (rs.next()){
tricketType=new TricketType();
tricketType.setStation(rs.getString("station"));
tricketType.setId(rs.getInt("id"));
this.s_startStationBox.addItem(tricketType);
this.s_endStationBox.addItem(tricketType);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void resetValue(){
this.tricketIdTxt.setText("");
this.startStationTxt.setText("");
this.endStationTxt.setText("");
this.seatTypeTxt.setText("");
this.priceTxt.setText("");
this.startTimeTxt.setText("");
this.endTimeTxt.setText("");
this.remarksTxt.setText("");
}
private void fillTable(Tricket tricket){
DefaultTableModel dtm =(DefaultTableModel) resultTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=conn.getCon();
ResultSet rs=tricketdao.list(con,tricket);
while(rs.next()){
Vector<java.io.Serializable> v=new Vector<>();
v.add(rs.getString("id"));
v.add(rs.getString("startStation"));
v.add(rs.getString("endStation"));
v.add(rs.getString("seat"));
v.add(rs.getFloat("price"));
v.add(rs.getDate("startTime"));
v.add(rs.getDate("arriveTime"));
v.add(rs.getString("remarks"));
dtm.addRow(v);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
conn.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// Variables declaration - do not modify
private javax.swing.JButton deleteButton;
private javax.swing.JTextField endStationTxt;
private javax.swing.JTextField endTimeTxt;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField priceTxt;
private javax.swing.JTextField remarksTxt;
private javax.swing.JTextField seatTypeTxt;
private javax.swing.JButton selectButton;
private javax.swing.JTextField startStationTxt;
private javax.swing.JTextField startTimeTxt;
private javax.swing.JTextField tricketIdTxt;
private javax.swing.JButton updateButton;
// End of variables declaration
}
4.3 售票管理功能数据库实现
package dao;
import 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();
}
}
5. 数据库部分
5.1数据库部分展示
6 项目结构
结束
只是部分代码,借鉴的话足够了,想要运行的话还差好多,太长了。这都50000多字了先算了
这个里面已经包含了,数据库操作和java逻辑的实现,学习的话足够了,源码 在下面网盘链接:是全部的代码了,拿了请留下你的关注个赞谢谢。
–>网盘链接:https://pan.baidu.com/s/1CD3gFm68jeA0pEvQKriexg 验证码:yyds
今天的文章java火车售票系统(含网盘链接)分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/31814.html