Java课设:火车售票系统2.0(代码)

Access.java

package TraSystem;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
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;

	//Main
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Access frame = new Access();
					frame.setVisible(true);
					frame.setResizable(false); 
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

    //创建界面
	public Access() {
		setTitle("\u6B22\u8FCE\u4F7F\u7528\u706B\u8F66\u552E\u7968\u7CFB\u7EDF\uFF01");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(600, 300, 450, 355);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//标签设置
		JLabel label = new JLabel("\u7528\u6237\u767B\u5F55");
		label.setBounds(148, 30, 120, 40);
		label.setFont(new Font("微软雅黑", Font.BOLD, 30));
		JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");
		lblNewLabel.setBounds(59, 99, 45, 18);
		JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801");
		lblNewLabel_1.setBounds(59, 181, 30, 18);
		
		textField = new JTextField();
		textField.setBounds(127, 96, 165, 24);
		textField.setColumns(10);
		passwordField = new JPasswordField();
		passwordField.setBounds(127, 178, 165, 24);
		
		//登录按钮
		JButton LoginInButton = new JButton("\u767B\u9646");
		LoginInButton.setBounds(127, 251, 70, 29);
		LoginInButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		LoginInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String user = textField.getText();
				String pass = new String(passwordField.getPassword());
				try{
					boolean com = UserLoginIn(user,pass);     //调用UserLoginIn函数
					if(com){
					    JOptionPane.showMessageDialog(null, "用户登录成功!");
					    UserMainFrame umf = new UserMainFrame(user);
					    umf.setVisible(true);
					    umf.setResizable(false);
					    dispose();
					}
					else{JOptionPane.showMessageDialog(null, "用户登录失败!");}
				}
				catch(Exception e1){
					e1.printStackTrace();
				}
			}
		});
		
		
		//注册按钮
		JButton RegisterButton = new JButton("\u6CE8\u518C");
		RegisterButton.setBounds(229, 251, 70, 29);
		RegisterButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		RegisterButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				RegisterFrame rf = new RegisterFrame();
				rf.setVisible(true);
				rf.setResizable(false);
			}
		});
		
		
		//管理员登陆按钮
		JButton SaLoginInButton = new JButton("\u7BA1\u7406\u5458\u767B\u9646");
		SaLoginInButton.setBounds(306, 175, 112, 29);
		SaLoginInButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		SaLoginInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String user = textField.getText();
				String pass = new String(passwordField.getPassword());
				try{
					boolean com = SaLoginIn(user,pass);    //调用SaLoginIn函数
					if(com){
					    JOptionPane.showMessageDialog(null, "管理员登录成功!");
					    SaMainFrame smf = new SaMainFrame(user);
					    smf.setVisible(true);
					    smf.setResizable(false);
					    dispose();
					}
					else{JOptionPane.showMessageDialog(null, "管理员登录失败!");}
				}
				catch(Exception e1){
					e1.printStackTrace();
				}
			}
		});
		contentPane.setLayout(null);
		contentPane.add(lblNewLabel);
		contentPane.add(lblNewLabel_1);
		contentPane.add(passwordField);
		contentPane.add(textField);
		contentPane.add(LoginInButton);
		contentPane.add(RegisterButton);
		contentPane.add(SaLoginInButton);
		contentPane.add(label);
	}
	
	//判断用户登陆
    boolean UserLoginIn(String accountT,String passwordT) 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 account = rs.getString(1);
		    String password = rs.getString(2);
		    if(account.equals(accountT) && password.equals(passwordT)){
			    return true;
		    }
	    }
	    return false;
    }

    //判断管理员登陆
    boolean SaLoginIn(String saaccountT,String passwordT) 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 saaccount = rs.getString(1);
		    String password = rs.getString(2);
		    if(saaccount.equals(saaccountT) && password.equals(passwordT)){
			    return true;
		    }
	    }
	    return false;
    }

}

AddFrame.java

package TraSystem;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;

	//创建录入界面
	public AddFrame(){
		setTitle("\u5F55\u5165\u8F66\u6B21");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(600, 300, 450, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//label
		JLabel label = new JLabel("\u8F66\u6B21\u53F7\uFF1A");
		label.setBounds(66, 46, 64, 18);
		label.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_1 = new JLabel("\u59CB\u53D1\u5730\uFF1A");
		label_1.setBounds(66, 88, 64, 18);
		label_1.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_2 = new JLabel("\u76EE\u7684\u5730\uFF1A");
		label_2.setBounds(66, 130, 64, 18);
		label_2.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_3 = new JLabel("\u51FA\u53D1\u65F6\u95F4\uFF1A");
		label_3.setBounds(50, 172, 80, 18);
		label_3.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_4 = new JLabel("\u5269\u4F59\u7968\u6570\uFF1A");
		label_4.setBounds(50, 214, 80, 18);
		label_4.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_5 = new JLabel("\u662F\u5426\u9AD8\u94C1\uFF1A");
		label_5.setBounds(50, 251, 80, 18);
		label_5.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_6 = new JLabel("\u7968\u4EF7\uFF1A");
		label_6.setBounds(82, 293, 48, 18);
		label_6.setFont(new Font("宋体", Font.BOLD, 15));
		
		textField = new JTextField();
		textField.setBounds(136, 43, 200, 24);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(136, 85, 200, 24);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(136, 127, 200, 24);
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(136, 169, 200, 24);
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setBounds(136, 211, 200, 24);
		textField_4.setColumns(10);
		
		textField_5 = new JTextField();
		textField_5.setBounds(136, 248, 200, 24);
		textField_5.setColumns(10);
		
		textField_6 = new JTextField();
		textField_6.setBounds(136, 290, 200, 24);
		textField_6.setColumns(10);
		
		//录入按钮
		JButton button = new JButton("\u5F55\u5165");
		button.setBounds(175, 356, 70, 29);
		button.setFont(new Font("微软雅黑", Font.BOLD, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == button){
					String a = textField.getText();
					String b = textField_1.getText();
					String c = textField_2.getText();
					String d = textField_3.getText();
					int f = Integer.valueOf(textField_4.getText());
					String g = textField_5.getText();
					String h = textField_6.getText();
				    new Add(a,b,c,d,f,g,h);
			    }
			}
		});
		contentPane.setLayout(null);
		contentPane.add(label_1);
		contentPane.add(label);
		contentPane.add(label_2);
		contentPane.add(label_5);
		contentPane.add(label_4);
		contentPane.add(label_3);
		contentPane.add(label_6);
		contentPane.add(textField);
		contentPane.add(textField_1);
		contentPane.add(textField_2);
		contentPane.add(textField_3);
		contentPane.add(textField_4);
		contentPane.add(textField_5);
		contentPane.add(textField_6);
		contentPane.add(button);
	}
}

Add.java

package TraSystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class Add {
	private Connection conn;
	
	public Add(String a,String b,String c,String d,int f,String g,String h){
		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+"','"+c+"','"+d+"',"+f+",'"+g+"','"+h+"')";	
	        java.sql.Statement stmt = conn.createStatement();
	        stmt.executeUpdate(sql);
	        conn.close();
	        JOptionPane.showMessageDialog(null, "录入成功!");
	    }
		catch (ClassNotFoundException e) {
	        JOptionPane.showMessageDialog(null, "录入失败!数据库连接失败!");
	        e.printStackTrace();
	    }
		catch (SQLException e) {
	        JOptionPane.showMessageDialog(null, "录入失败!输入信息有误或表信息出错!");
	        e.printStackTrace();
	    }  	
	}

}

AllTrain.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.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 AllTrain extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	JTable table = new JTable();
	private Connection conn;
	public AllTrain(){
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		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 = query3();
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    }
	    this.getContentPane().add(new JScrollPane(table));
	    this.setVisible(true);
	    try{
	    	conn.close();
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    }
	}

    public JTable query3() throws SQLException{
	    DefaultTableModel tbmode=new DefaultTableModel();
	    String sql = "SELECT * FROM 车次信息";
	    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);
	}

}


EraseFrame.java

package TraSystem;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class EraseFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField textField;


	//创建删除界面
	public EraseFrame() {
		setTitle("\u5220\u9664\u754C\u9762");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(600, 300, 435, 120);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//label
		JLabel label = new JLabel("\u8F66\u6B21\u53F7\uFF1A");
		label.setBounds(32, 33, 64, 18);
		label.setFont(new Font("宋体", Font.BOLD, 15));
		textField = new JTextField();
		textField.setBounds(114, 30, 162, 24);
		textField.setColumns(10);
		
		//删除按钮
		JButton btnNewButton = new JButton("\u5220\u9664");
		btnNewButton.setBounds(309, 27, 70, 29);
		btnNewButton.setForeground(Color.RED);
		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String a = textField.getText();
				new Erase(a);
			}
		});
		contentPane.setLayout(null);
		contentPane.add(label);
		contentPane.add(textField);
		contentPane.add(btnNewButton);
	}
}


Erase.java

package TraSystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class Erase {
	private Connection conn;
	
	public Erase(String a){
        try{
        	boolean j = Judge(a);
        	if(j == false){
        		JOptionPane.showMessageDialog(null, "删除失败!不存在此车次号!");
        	}
        	else{
        		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            	conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
            	String sql = "delete from 车次信息 where 车次号='"+a+"'";	
            	Statement stmt = conn.createStatement();
            	stmt.executeUpdate(sql);
            	conn.close();
            	JOptionPane.showMessageDialog(null, "删除成功!");
        	}
        }
        catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "删除失败!数据库连接失败!");
        	e.printStackTrace();
        }
        catch (SQLException e) {
        	JOptionPane.showMessageDialog(null, "删除失败!表出错!");
        	e.printStackTrace();
        }
        catch (Exception e) {
        	JOptionPane.showMessageDialog(null, "删除失败!连接数据库失败!");
			e.printStackTrace();
		}
	}
	
	boolean Judge(String a) throws Exception{
		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();
    	String sql = "SELECT * FROM 车次信息";
	    ResultSet rs = stmt.executeQuery(sql);
	    while(rs.next()){
	    	String text = rs.getString(1);
	    	if (text.equals(a))
	    		return true;
	    }
		return false;
	}

}


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.setFont(new Font("微软雅黑", Font.BOLD, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 PurchaseFrame pf = new PurchaseFrame(user);
				 pf.setVisible(true);
			}
		});
		
		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;
	
	//创建查询2界面
	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.setFont(new Font("微软雅黑", Font.BOLD, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 PurchaseFrame pf = new PurchaseFrame(user);
				 pf.setVisible(true);
			}
		});
		
		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);
	}
    
}

PurchaseFrame.java

package TraSystem;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
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;

	//创建购买界面
	public PurchaseFrame(String user) {
		setTitle("\u8D2D\u4E70\u754C\u9762");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(650, 300, 434, 122);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//label
		JLabel label = new JLabel("\u8F66\u6B21\u53F7\uFF1A");
		label.setBounds(28, 32, 64, 18);
		label.setFont(new Font("宋体", Font.BOLD, 15));
		
		textField = new JTextField();
		textField.setBounds(110, 29, 183, 24);
		textField.setColumns(10);
		
		//购票按钮
		JButton button = new JButton("\u8D2D\u7968");
		button.setBounds(311, 26, 70, 29);
		button.setForeground(Color.BLUE);
		button.setFont(new Font("微软雅黑", Font.BOLD, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String a = textField.getText();
				new Purchase(a,user);
			}
		});
		contentPane.setLayout(null);
		contentPane.add(label);
		contentPane.add(textField);
		contentPane.add(button);
	}

}

Purchase.java

package TraSystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class Purchase {
	private Connection conn;
	
	public Purchase(String a,String user){
		try{
			boolean j = Judge(a);
        	if(j == false){
        		JOptionPane.showMessageDialog(null, "购票失败!不存在此车次号!");
        	}
        	else{
        		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();
         } catch (Exception e) {
        	 JOptionPane.showMessageDialog(null, "购票失败!连接数据库失败!");
			e.printStackTrace();
		}      	
	}
	
	boolean Judge(String a) throws Exception{
		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();
    	String sql = "SELECT * FROM 车次信息";
	    ResultSet rs = stmt.executeQuery(sql);
	    while(rs.next()){
	    	String text = rs.getString(1);
	    	if (text.equals(a))
	    		return true;
	    }
		return false;
	}
	
}

RefundFrame.java

package TraSystem;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RefundFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	
	//创建退票界面
	public RefundFrame(String user) {
		setTitle("\u9000\u7968\u754C\u9762");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(650, 300, 450, 117);
		contentPane = new JPanel();
		contentPane.setForeground(Color.BLUE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//label
		JLabel label = new JLabel("\u8F66\u6B21\u53F7\uFF1A");
		label.setBounds(37, 24, 64, 18);
		label.setFont(new Font("宋体", Font.BOLD, 15));
		
		textField = new JTextField();
		textField.setBounds(119, 21, 180, 24);
		textField.setColumns(10);
		
		//退票按钮
		JButton btnNewButton = new JButton("\u9000\u7968");
		btnNewButton.setBounds(326, 18, 70, 29);
		btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		btnNewButton.setForeground(Color.RED);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String a = textField.getText();
				new Refund(a,user);
			}
		});
		contentPane.setLayout(null);
		contentPane.add(label);
		contentPane.add(textField);
		contentPane.add(btnNewButton);
	}
}

Refund.java

package TraSystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class Refund {
	private Connection conn;
	
	public Refund(String a,String user){
        try{
        	boolean j = Judge(a,user);
        	if(j == false){
        		JOptionPane.showMessageDialog(null, "退票失败!不存在此车次号!");
        	}
        	else{
        		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();
        }
        catch (Exception e) {
        	JOptionPane.showMessageDialog(null, "退票失败!数据库连接失败!");
			e.printStackTrace();
		}
	}
	
	boolean Judge(String a,String user) throws Exception{
		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();
    	String sql = "SELECT * FROM "+user+"订购信息";
	    ResultSet rs = stmt.executeQuery(sql);
	    while(rs.next()){
	    	String text = rs.getString(1);
	    	if (text.equals(a))
	    		return true;
	    }
		return false;
	}

}

RegisterFrame.java

package TraSystem;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
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.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);
		
		//label
		JLabel label = new JLabel("\u8F93\u5165\u7528\u6237\u540D\uFF08\u6700\u957F10\u4E2A\u5B57\u7B26\uFF09");
		label.setBounds(19, 117, 198, 20);
		label.setFont(new Font("微软雅黑", Font.BOLD, 15));
		
		JLabel label_1 = new JLabel("\u7528\u6237\u6CE8\u518C");
		label_1.setBounds(157, 38, 100, 34);
		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.setBounds(19, 194, 183, 20);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
		
		textField = new JTextField();
		textField.setBounds(235, 113, 148, 24);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(235, 193, 149, 24);
		textField_1.setColumns(10);
		
		//注册按钮
		JButton button = new JButton("\u6CE8\u518C");
		button.setBounds(166, 249, 79, 29);
		button.setFont(new Font("微软雅黑", Font.BOLD, 15));
		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();
				}
			}
		});
		contentPane.setLayout(null);
		contentPane.add(label_1);
		contentPane.add(label);
		contentPane.add(label_2);
		contentPane.add(textField_1);
		contentPane.add(textField);
		contentPane.add(button);
	}

}

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(sql2);
        	stmt.executeUpdate(sql);
        	conn.close();
        	JOptionPane.showMessageDialog(null, "注册成功!");
        } 
        catch (ClassNotFoundException e) {
        	JOptionPane.showMessageDialog(null, "注册失败!数据库连接失败!");
        	e.printStackTrace();
        }	
        catch (SQLException e) {
        	JOptionPane.showMessageDialog(null, "注册失败!用户名已使用或表信息出错!");
        	e.printStackTrace();
        }
	}
}

SaMainFrame.java

package TraSystem;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SaMainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;

	//创建管理员界面
	public SaMainFrame(String user) {
		setTitle("\u6B22\u8FCE\uFF0C\u7BA1\u7406\u5458\uFF01");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(600, 300, 450, 300);
		
		//菜单栏
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("\u76F8\u5173");
		menuBar.add(menu);
		
		//注销菜单按钮
		JMenuItem menuItem = new JMenuItem("\u6CE8\u9500");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "注销成功!");
				dispose();
				Access acc = new Access();
				acc.setVisible(true);
				acc.setResizable(false);
			}
		});
		
		//用户信息菜单按钮
		JMenuItem menuItem_1 = new JMenuItem("\u7528\u6237\u8D44\u6599\u67E5\u8BE2");
		menuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserInfo uf = new UserInfo();
				uf.setVisible(true);
			}
		});
		menu.add(menuItem_1);
		menu.add(menuItem);
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//label
		JLabel label = new JLabel("\u6B22\u8FCE\u6765\u5230\u7BA1\u7406\u5458\u6A21\u5F0F");
		label.setBounds(99, 40, 216, 33);
		label.setFont(new Font("微软雅黑", Font.BOLD, 24));
		
		//录入按钮
		JButton AddButton = new JButton("\u5F55\u5165\u8F66\u6B21");
		AddButton.setBounds(75, 95, 100, 29);
		AddButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		AddButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddFrame af = new AddFrame();
				af.setVisible(true);
				af.setResizable(false);
			}
		});
		
		//修改按钮
		JButton UpdateButton = new JButton("\u4FEE\u6539\u8F66\u6B21\u4FE1\u606F");
		UpdateButton.setBounds(214, 95, 127, 29);
		UpdateButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		UpdateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UpdateFrame uf = new UpdateFrame();
				uf.setVisible(true);
				uf.setResizable(false);
			}
		});
		
		//查询按钮
		JButton FindButton = new JButton("\u67E5\u770B\u6240\u6709\u8F66\u6B21");
		FindButton.setBounds(75, 151, 127, 29);
		FindButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		FindButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AllTrain at = new AllTrain();
				at.setVisible(true);
			}
		});
		
		//删除按钮
		JButton DeleteButton = new JButton("\u5220\u9664\u8F66\u6B21");
		DeleteButton.setBounds(216, 151, 99, 29);
		DeleteButton.setFont(new Font("微软雅黑", Font.BOLD, 15));
		DeleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EraseFrame ef = new EraseFrame();
				ef.setVisible(true);
				ef.setResizable(false);
			}
		});
		contentPane.setLayout(null);
		contentPane.add(label);
		contentPane.add(FindButton);
		contentPane.add(DeleteButton);
		contentPane.add(AddButton);
		contentPane.add(UpdateButton);
	}
}

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=query3(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 query3(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);
	}
    
}

UpdateFrame.java

package TraSystem;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class UpdateFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	//创建修改界面
	public UpdateFrame() {
		setTitle("\u4FEE\u6539\u8F66\u6B21\u4FE1\u606F");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(600, 300, 601, 404);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//label
		JLabel label = new JLabel("\u8F93\u5165\u5C5E\u6027\uFF1A");
		label.setBounds(81, 127, 80, 18);
		label.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_1 = new JLabel("\u8F93\u5165\u5185\u5BB9\uFF1A");
		label_1.setBounds(81, 213, 80, 18);
		label_1.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel lblNewLabel = new JLabel("\u8F93\u5165\u8F66\u6B21\u53F7\uFF1A");
		lblNewLabel.setBounds(81, 71, 96, 18);
		lblNewLabel.setFont(new Font("宋体", Font.BOLD, 15));
		
		JLabel label_2 = new JLabel("\uFF08\u5C5E\u6027\u540D\uFF1A\u59CB\u53D1\u5730\uFF0C\u76EE\u7684\u5730\uFF0C\u51FA\u53D1\u65F6\u95F4\uFF0C\u5269\u4F59\u7968\u6570\uFF0C\u662F\u5426\u9AD8\u94C1\uFF0C\u7968\u4EF7\uFF09");
		label_2.setBounds(73, 274, 465, 18);
		label_2.setForeground(Color.RED);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
		
		textField = new JTextField();
		textField.setBounds(202, 124, 187, 24);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(202, 210, 187, 24);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(202, 68, 187, 24);
		textField_2.setColumns(10);
		
		//修改按钮
		JButton button = new JButton("\u4FEE\u6539");
		button.setBounds(216, 310, 70, 29);
		button.setFont(new Font("微软雅黑", Font.BOLD, 15));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == button){
					String a = textField.getText();
					String b = textField_1.getText();
					String c = textField_2.getText();
					if (b == "剩余票数"){
						new Update(a,b,c,1);
					}
					else{
						new Update(a,b,c,0);
					}
					
			    }
			}
		});
		contentPane.setLayout(null);
		contentPane.add(label);
		contentPane.add(lblNewLabel);
		contentPane.add(label_1);
		contentPane.add(textField_1);
		contentPane.add(textField);
		contentPane.add(textField_2);
		contentPane.add(label_2);
		contentPane.add(button);
	}
}

Update.java

package TraSystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class Update {
	private Connection conn;
	
	public Update(String a,String b,String c,int d){
		try{
	        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	        conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName=火车售票系统","sa","w12621058");
	        String sql0 = "UPDATE 车次信息 SET "+a+"='"+b+"' WHERE 车次号='"+c+"'";
	        String sql1 = "UPDATE 车次信息 SET "+a+"="+b+" WHERE 车次号='"+c+"'";
	        java.sql.Statement stmt = conn.createStatement();
	        if(d == 0){
	        	stmt.executeUpdate(sql0);
	        }
	        else{
	        	stmt.executeUpdate(sql1);
	        }
	        conn.close();
	        JOptionPane.showMessageDialog(null, "修改成功!");
	    }
		catch (ClassNotFoundException e) {
	        JOptionPane.showMessageDialog(null, "修改失败!数据库连接出错!");
	        e.printStackTrace();
	    }
		catch (SQLException e) {
	        JOptionPane.showMessageDialog(null, "修改失败!输入信息错误或表信息错误!");
	        e.printStackTrace();
	    }
	}

}

UserInfo.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.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 UserInfo extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JTable table = new JTable();
	private Connection conn;
	
	//创建用户信息界面
	public UserInfo(){
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		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 = query4();
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    }
	    this.getContentPane().add(new JScrollPane(table));
	    this.setVisible(true);
	    try{
	    	conn.close();
	    }
	    catch(Exception e){
	    	e.printStackTrace();
	    }
	}

	//获取表函数
    public JTable query4() throws SQLException{
	    DefaultTableModel tbmode=new DefaultTableModel();
	    String sql = "SELECT * FROM 用户信息";
	    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);
	}
    
}

UserMainFrame.java

package TraSystem;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
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 UserMainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	//创建主界面
	public UserMainFrame(String user) {
		setTitle("\u706B\u8F66\u552E\u7968\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(600, 300, 450, 400);
		
		//菜单栏
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("\u7528\u6237\u4FE1\u606F");
		menuBar.add(menu);
		
		//购票信息按钮
		JMenuItem menuItem = new JMenuItem("\u60A8\u7684\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) {
				RefundFrame rf = new RefundFrame(user);
				rf.setVisible(true);
				rf.setResizable(false);
			}
		});
		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 acc = new Access();
				acc.setVisible(true);
				acc.setResizable(false);
			}
		});
		menu.add(menuItem_2);
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		//label
		JLabel lblNewLabel = new JLabel("\u8F66\u6B21\u67E5\u8BE2");
		lblNewLabel.setBounds(145, 5, 128, 43);
		lblNewLabel.setFont(new Font("黑体", Font.BOLD, 30));
		
        JLabel label = new JLabel("\u59CB\u53D1\u5730");
        label.setBounds(58, 83, 45, 18);
		
		JLabel label_1 = new JLabel("\u76EE\u7684\u5730");
		label_1.setBounds(312, 83, 45, 18);
		
		textField = new JTextField();
		textField.setBounds(41, 119, 86, 24);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(284, 119, 97, 24);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setBounds(59, 256, 142, 24);
		textField_2.setColumns(10);
		
		//查询按钮
		JButton Find1Button = new JButton("\u5F00\u59CB\u67E5\u8BE2");
		Find1Button.setBounds(159, 188, 97, 29);
		Find1Button.setFont(new Font("微软雅黑", Font.BOLD, 15));
		Find1Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String sfd = textField.getText();
				String mdd = textField_1.getText();
				new Find1(sfd,mdd,user);
			}
		});
		
		
		//查询2按钮
		JButton Find2Button = new JButton("\u7279\u5B9A\u8F66\u6B21\u53F7\u67E5\u8BE2");
		Find2Button.setBounds(244, 253, 142, 29);
		Find2Button.setFont(new Font("微软雅黑", Font.BOLD, 15));
		Find2Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String a = textField_2.getText();
				new Find2(a,user);
			}
		});
		contentPane.setLayout(null);
		contentPane.add(textField);
		contentPane.add(label);
		contentPane.add(textField_2);
		contentPane.add(textField_1);
		contentPane.add(Find2Button);
		contentPane.add(label_1);
		contentPane.add(Find1Button);
		contentPane.add(lblNewLabel);
	}
}
  • 25
    点赞
  • 203
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值