文章目录
相关请查看另一博客注意事项。
如果有一些问题的话可能之后会改进,也会适当增加一些功能。
登陆界面Access.java
package TraSystem;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Access extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Access frame = new Access();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Access() {
setTitle("\u6B22\u8FCE\u4F7F\u7528\u706B\u8F66\u552E\u7968\u7CFB\u7EDF\uFF01");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(600, 300, 450, 340);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u7528\u6237\u767B\u5F55");
label.setFont(new Font("微软雅黑", Font.BOLD, 30));
JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");
JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801");
textField = new JTextField();
textField.setColumns(10);
passwordField = new JPasswordField();
JButton btnNewButton = new JButton("\u767B\u9646");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user=textField.getText();
String pass=new String(passwordField.getPassword());
try{
boolean com=compareWithSql(user,pass);
if(com){
JOptionPane.showMessageDialog(null, "登录成功!");
FindMain t1=new FindMain(user);
t1.setVisible(true);
dispose();
}
else{JOptionPane.showMessageDialog(null, "登录失败!");}
}
catch(Exception e1){
e1.printStackTrace();
}
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
JButton button = new JButton("\u6CE8\u518C");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
RegisterFrame r=new RegisterFrame();
r.setVisible(true);
}
});
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(54)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblNewLabel)
.addComponent(lblNewLabel_1))
.addGap(23)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(passwordField)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
.addComponent(btnNewButton)
.addComponent(button, Alignment.TRAILING)))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(143)
.addComponent(label)))
.addContainerGap(135, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(25)
.addComponent(label)
.addGap(26)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(55)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblNewLabel_1)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(47)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(btnNewButton)
.addComponent(button))
.addContainerGap())
);
contentPane.setLayout(gl_contentPane);
}
boolean compareWithSql(String accountT,String namesT) throws Exception{
String sql;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
Statement stmt = conn.createStatement();
sql = "select * from 用户信息";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String acc = rs.getString(1);
String names = rs.getString(2);
if(acc.equals(accountT) && names.equals(namesT)){
return true;
}
}
return false;
}
}
注册Register.java
package TraSystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Register {
private Connection conn;
public Register(String a,String b){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
String sql="INSERT INTO 用户信息 VALUES('"+a+"','"+b+"')";
String sql2="CREATE TABLE "+a+"订购信息(车次号 varchar(10),始发地 varchar(20),目的地 varchar(20),出发时间 varchar(30),PRIMARY KEY(车次号),FOREIGN KEY(车次号)REFERENCES 车次信息(车次号))";
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.executeUpdate(sql2);
conn.close();
JOptionPane.showMessageDialog(null, "注册成功!");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "注册失败!");
e.printStackTrace();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "注册失败!");
e.printStackTrace();
}
}
}
注册界面RegisterFrame.java
package TraSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class RegisterFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public RegisterFrame() {
setTitle("\u7528\u6237\u6CE8\u518C");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(650, 300, 450, 350);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u8F93\u5165\u7528\u6237\u540D\uFF08\u6700\u957F10\u4E2A\u5B57\u7B26\uFF09");
label.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label_1 = new JLabel("\u7528\u6237\u6CE8\u518C");
label_1.setFont(new Font("微软雅黑", Font.BOLD, 25));
JLabel label_2 = new JLabel("\u8F93\u5165\u5BC6\u7801\uFF08\u6700\u957F10\u4E2A\u5B57\u7B26\uFF09");
label_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
JButton button = new JButton("\u6CE8\u518C");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a=textField.getText();
String b=textField_1.getText();
if(a.equals("")||b.equals("")){
JOptionPane.showMessageDialog(null, "请输入信息!");
}
else{
new Register(a,b);
dispose();
}
}
});
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(152)
.addComponent(label_1))
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addComponent(label_2))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
.addComponent(textField, 148, 148, 148))))
.addContainerGap(43, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(173)
.addComponent(button)
.addContainerGap(186, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(33)
.addComponent(label_1)
.addGap(41)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(label)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(56)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
.addComponent(button)
.addContainerGap())
);
contentPane.setLayout(gl_contentPane);
}
}
查找主界面FindMain.java
package TraSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class FindMain extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
public FindMain(String user) {
setTitle("\u706B\u8F66\u552E\u7968\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(600, 300, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("\u7528\u6237\u4FE1\u606F");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("\u5DF2\u8D2D\u8F66\u7968");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Ticket(user);
}
});
menu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("\u9000\u7968\u7CFB\u7EDF");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DeleteFrame d1=new DeleteFrame(user);
d1.setVisible(true);
}
});
menu.add(menuItem_1);
JMenuItem menuItem_2 = new JMenuItem("\u6CE8\u9500");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "注销成功!");
dispose();
Access frame = new Access();
frame.setVisible(true);
}
});
menu.add(menuItem_2);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("\u8F66\u6B21\u67E5\u8BE2");
lblNewLabel.setFont(new Font("黑体", Font.BOLD, 30));
textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
JButton button = new JButton("\u5F00\u59CB\u67E5\u8BE2");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String sfd=textField.getText();
String mdd=textField_1.getText();
new Find1(sfd,mdd,user);
}
});
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
JLabel label = new JLabel("\u59CB\u53D1\u5730");
JLabel label_1 = new JLabel("\u76EE\u7684\u5730");
JButton button_1 = new JButton("\u7279\u5B9A\u8F66\u6B21\u53F7\u67E5\u8BE2");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Find2Frame b=new Find2Frame(user);
b.setVisible(true);
}
});
button_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(142)
.addComponent(lblNewLabel)
.addGap(152))
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(36)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(53)
.addComponent(label)))
.addPreferredGap(ComponentPlacement.RELATED, 165, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(49))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label_1)
.addGap(81))))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(75)
.addComponent(button)
.addGap(53)
.addComponent(button_1)
.addContainerGap(88, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
.addGap(35)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(label_1))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button_1)
.addComponent(button))
.addContainerGap())
);
contentPane.setLayout(gl_contentPane);
}
}
输入起点终点查找车次Find1.java
package TraSystem;
import java.awt.BorderLayout;
import java.awt.Font;
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.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class Find1 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTable table=new JTable();
private Connection conn;
public Find1(String sfd,String mdd,String user){
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton button = new JButton("\u8D2D\u7968");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PurchaseFrame pf=new PurchaseFrame(user);
pf.setVisible(true);
}
});
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
contentPane.add(button, BorderLayout.SOUTH);
this.setBounds(650,300,800,200);
this.setTitle("车次信息");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
table=query(sfd,mdd);
} catch(Exception e){e.printStackTrace();}
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
try{conn.close();}catch(Exception e){e.printStackTrace();}
}
public JTable query(String start,String end) throws SQLException{
DefaultTableModel tbmode=new DefaultTableModel();
String sql="SELECT * FROM 车次信息 WHERE 始发地='"+start+"' AND 目的地='"+end+"'";
try{
Statement Stmt=conn.createStatement();
ResultSet rs= Stmt.executeQuery(sql);
ResultSetMetaData meta=rs.getMetaData();
int colcount=meta.getColumnCount();
for(int i=1;i<=colcount;i++)
tbmode.addColumn(meta.getColumnName(i));
Object[]col=new Object[colcount];
while(rs.next()){
for(int j=1;j<=col.length;j++)
col[j-1]=rs.getString(j);
tbmode.addRow(col);
}
rs.close();
Stmt.close();
}catch(Exception e){e.printStackTrace();}
return new JTable(tbmode);
}
}
输入车次号查找车次Find2.java
package TraSystem;
import java.awt.BorderLayout;
import java.awt.Font;
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.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class Find2 extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTable table=new JTable();
private Connection conn;
public Find2(String a,String user){
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton button = new JButton("\u8D2D\u7968");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
PurchaseFrame pf=new PurchaseFrame(user);
pf.setVisible(true);
}
});
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
contentPane.add(button, BorderLayout.SOUTH);
this.setBounds(650,300,800,200);
this.setTitle("车次信息");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
table=query2(a);
} catch(Exception e){e.printStackTrace();}
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
try{conn.close();}catch(Exception e){e.printStackTrace();}
}
public JTable query2(String a) throws SQLException{
DefaultTableModel tbmode=new DefaultTableModel();
String sql="SELECT * FROM 车次信息 WHERE 车次号='"+a+"'";
try{
Statement Stmt=conn.createStatement();
ResultSet rs= Stmt.executeQuery(sql);
ResultSetMetaData meta=rs.getMetaData();
int colcount=meta.getColumnCount();
for(int i=1;i<=colcount;i++)
tbmode.addColumn(meta.getColumnName(i));
Object[]col=new Object[colcount];
while(rs.next()){
for(int j=1;j<=col.length;j++)
col[j-1]=rs.getString(j);
tbmode.addRow(col);
}
rs.close();
Stmt.close();
}catch(Exception e){e.printStackTrace();}
return new JTable(tbmode);
}
}
输入车次号查找的界面Find2Frame.java
package TraSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Find2Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public Find2Frame(String user) {
setTitle("\u8F66\u6B21\u67E5\u8BE2");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(650, 300, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u76F4\u63A5\u8F93\u5165\u9700\u8981\u67E5\u8BE2\u7684\u8F66\u6B21\u53F7");
label.setForeground(Color.BLUE);
label.setFont(new Font("微软雅黑", Font.BOLD, 25));
textField = new JTextField();
textField.setColumns(10);
JButton button = new JButton("\u67E5\u8BE2");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String a=textField.getText();
new Find2(a,user);
}
});
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(115)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(174)
.addComponent(button))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(61)
.addComponent(label)))
.addContainerGap(61, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(39)
.addComponent(label)
.addGap(53)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
.addComponent(button)
.addContainerGap())
);
contentPane.setLayout(gl_contentPane);
}
}
购票Purchase.java
package TraSystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Purchase {
private Connection conn;
public Purchase(String a,String user){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
String sql="insert into "+user+"订购信息 select 车次号,始发地,目的地,出发时间 from 车次信息 where 车次号='"+a+"' group by 车次号,始发地,目的地,出发时间";
String sql2="update 车次信息 set 剩余票数=剩余票数-1 where 车次号='"+a+"'"; //更新车次信息
java.sql.Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.executeUpdate(sql2);
conn.close();
JOptionPane.showMessageDialog(null, "购票成功!");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "购票失败!");
e.printStackTrace();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "购票失败!");
e.printStackTrace();
}
}
}
购票界面PurchaseFrame.java
package TraSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class PurchaseFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
/**
* Create the frame.
*/
public PurchaseFrame(String user) {
setTitle("\u8D2D\u4E70");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(650, 300, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u8F93\u5165\u60A8\u8981\u8D2D\u4E70\u7684\u8F66\u6B21\u53F7");
label.setForeground(Color.BLUE);
label.setFont(new Font("微软雅黑", Font.BOLD, 30));
textField = new JTextField();
textField.setColumns(10);
JButton button = new JButton("\u8D2D\u7968");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a=textField.getText();
new Purchase(a,user);
}
});
button.setForeground(Color.BLUE);
button.setFont(new Font("微软雅黑", Font.BOLD, 15));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(176)
.addComponent(button))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(104)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(61)
.addComponent(label)))
.addContainerGap(61, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(32)
.addComponent(label)
.addGap(52)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
.addComponent(button)
.addContainerGap())
);
contentPane.setLayout(gl_contentPane);
}
}
显示已购买的车票Ticket.java
package TraSystem;
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class Ticket extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
JTable table=new JTable();
private Connection conn;
public Ticket(String user){
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
this.setBounds(600,300,800,300);
this.setTitle("已购车次");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
table=query(user);
} catch(Exception e){e.printStackTrace();}
this.getContentPane().add(new JScrollPane(table));
this.setVisible(true);
try{conn.close();}catch(Exception e){e.printStackTrace();}
}
public JTable query(String user) throws SQLException{
DefaultTableModel tbmode=new DefaultTableModel();
String sql="SELECT * FROM "+user+"订购信息";
try{
Statement Stmt=conn.createStatement();
ResultSet rs= Stmt.executeQuery(sql);
ResultSetMetaData meta=rs.getMetaData();
int colcount=4;
for(int i=1;i<=colcount;i++)
tbmode.addColumn(meta.getColumnName(i));
Object[]col=new Object[colcount];
while(rs.next()){
for(int j=1;j<=col.length;j++)
col[j-1]=rs.getString(j);
tbmode.addRow(col);
}
rs.close();
Stmt.close();
}catch(Exception e){e.printStackTrace();}
return new JTable(tbmode);
}
}
退票Delete.java
package TraSystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Delete {
private Connection conn;
public Delete(String a,String user){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
String sql="delete from "+user+"订购信息 where 车次号='"+a+"'";
String sql2="update 车次信息 set 剩余票数=剩余票数+1 where 车次号='"+a+"'"; //更新车次信息
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.executeUpdate(sql2);
conn.close();
JOptionPane.showMessageDialog(null, "退票成功!");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "退票失败!");
e.printStackTrace();
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "退票失败!");
e.printStackTrace();
}
}
}
退票界面DeleteFrame.java
package TraSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DeleteFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public DeleteFrame(String user) {
setTitle("\u9000\u7968\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(650, 300, 450, 300);
contentPane = new JPanel();
contentPane.setForeground(Color.BLUE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel label = new JLabel("\u8F93\u5165\u60A8\u8981\u9000\u7968\u7684\u8F66\u6B21\u53F7");
label.setForeground(Color.BLUE);
label.setFont(new Font("微软雅黑", Font.BOLD, 30));
textField = new JTextField();
textField.setColumns(10);
JButton btnNewButton = new JButton("\u9000\u7968");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String a=textField.getText();
new Delete(a,user);
}
});
btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
btnNewButton.setForeground(Color.BLUE);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addContainerGap(109, Short.MAX_VALUE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
.addGap(102))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(177)
.addComponent(btnNewButton)
.addContainerGap(182, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(61)
.addComponent(label)
.addContainerGap(61, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(29)
.addComponent(label)
.addGap(55)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
.addComponent(btnNewButton)
.addContainerGap())
);
contentPane.setLayout(gl_contentPane);
}
}