【归档】档案管理系统(java语言)

【归档】档案管理系统

Client.java

package file_system;

import java.io.*;
import java.net.*;
import java.sql.*;
import javax.swing.*;

public class Client {
	private static ObjectOutputStream output; // 发送到服务器的输出流
	private ObjectInputStream input; // 来自服务器的输入流
	private static String message = ""; // 从服务器接收到的消息
	private static String chatServer; //此应用程序的主机服务器
	private Socket client; // 与服务器通信的套接字

	// 连接到服务器并处理来自服务器的消息
	public void runClient() {
		try
		{
			connectToServer(); // 创建套接字进行连接
			getStreams(); // 获取输入和输出流
			processConnection(); // 进行连接
		} 
		catch (EOFException eofException) {
			System.out.println("\nClient terminated connection");
		} // end catch
		catch (IOException ioException) {
			ioException.printStackTrace();
		}
		finally {
			closeConnection(); // 关闭连接
		} 
	}

	// 连接到服务端
	private void connectToServer() throws IOException {
		System.out.println("Attempting connection\n");
		InetAddress iAddress = InetAddress.getLocalHost();
		chatServer = iAddress.getHostAddress();
		// 创建套接字以连接到服务端
		try {
			client = new Socket(chatServer, 12345);
		} catch (ConnectException e) {
			System.out.println(e.getMessage());
			System.exit(0);
		}
		System.out.println("Connected to: " + client.getInetAddress().getHostName());
	}

	// 获取发送和接收数据的流
	private void getStreams() throws IOException {
		// 为对象设置输出流
		output = new ObjectOutputStream(client.getOutputStream());
		output.flush(); // 刷新输出缓冲区以发送头信息
		// 为对象设置输入流
		input = new ObjectInputStream(client.getInputStream());
		System.out.println("\nGot I/O streams\n");
	}

	// 与服务器的进程连接
	private void processConnection() throws IOException {
		do //处理从服务器发送的消息
		{
			try
			{
				message = (String) input.readObject(); // 读取信息
				System.out.println("\n" + message); // 输出信息
			}
			catch (ClassNotFoundException classNotFoundException) {
				System.out.println("\nUnknown object type received");
			}

		} while (!message.equals("SERVER>>> TERMINATE"));
	}

	// 关闭流和套接字 
	private void closeConnection() {
		System.out.println("\nClosing connection");
		// 发送信息
		try {
			output.close(); // 关闭输出流
			input.close(); // 关闭输入流
			client.close(); // 关闭套接字
		}
		catch (IOException ioException) {
			System.out.println("关闭连接时出现异常:"+ioException.getMessage());
		}
	}

	// 向服务端发送消息
	public static void sendData(String message) {
		try
		{
			output.writeObject("CLIENT>>> " + message);
			output.flush(); // 将数据刷新到输出流
			System.out.println("\nCLIENT>>> " + message);
		}
		catch (IOException ioException) {
			System.out.println("\nError writing object");
		}
	}
	
	public static void main(String args[]) {
		try {
			DataProcessing.connectToDB();
			if (DataProcessing.getConnectedToDB()) {
				Client application;
				application = new Client( );
				LoginWindow window=new LoginWindow();
				window.loginFrame();//显示登录界面
				application.runClient();//运行客户端
			}
		}catch (ClassNotFoundException e) {
			JOptionPane.showMessageDialog(null,e.getMessage(), "数据驱动错误", JOptionPane.ERROR_MESSAGE);
		}catch (SQLException e) {
			JOptionPane.showMessageDialog(null,e.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
		} 
	}
}

DataProcessing.java

package file_system;

import java.sql.*;
import java.util.*;
import javax.swing.*;

public class DataProcessing{
	private static Connection connection;
	private static Statement statement;
	private static ResultSet resultSet;
	private static boolean connectedToDB=false;//标识数据库连接状态
	
	public static void connectToDB() throws ClassNotFoundException, SQLException {//连接数据库的方法
		String driverName="com.mysql.jdbc.Driver";// 加载数据库驱动类
		String url="jdbc:mysql://localhost:3306/document"; // 声明数据库的URL
		String user="root";// 数据库用户
		String password="******";// 数据库用户密码
		
		Class.forName(driverName);
		connection=DriverManager.getConnection(url, user, password);
        connectedToDB=true;
	}
	//封装连接状态的标识
	public static boolean getConnectedToDB() {
		return connectedToDB;
	}
	
	public static void disconnectFromDB() {//关闭数据库的连接
		if(connectedToDB) {
			try {
				resultSet.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null, e.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
			}finally {
				connectedToDB=false;
			}
		}
	}
	
	public static Doc searchDoc(String DocID) throws SQLException{//在数据库中查找指定ID文件的方法,返回Doc类
		Doc temp=null;
		if(!connectedToDB)
			throw new SQLException("Not Connected to Database.");
		statement=connection.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				ResultSet.CONCUR_READ_ONLY);//不能用结果集更新数据库中的表
		String sql="select * from doc_info where Id='"+DocID+"'";
		resultSet=statement.executeQuery(sql);
		if(resultSet.next()) {
			String ID=resultSet.getString("ID");
			String creator=resultSet.getString("creator");
			Timestamp timestamp=resultSet.getTimestamp("timestamp");
			String description=resultSet.getString("description");
			String filename=resultSet.getString("filename");
			temp=new Doc(ID,creator,timestamp,description,filename);
		}
		return temp;//返回Doc类
	}
	
	public static Enumeration<Doc> getAllDocs() throws SQLException{//在数据库中获取所有文件的方法,返回Doc枚举类
		Hashtable<String,Doc> docs=new Hashtable<String,Doc>();
		Doc temp=null;
		Enumeration<Doc> e;
		if(!connectedToDB) 
			throw new SQLException("Not Connected to Database.");
		statement=connection.createStatement(
				ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				ResultSet.CONCUR_READ_ONLY);//不能用结果集更新数据库中的表
	    String sql="select * from doc_info";
	    resultSet=statement.executeQuery(sql);
	    while(resultSet.next()) {
	    	String ID=resultSet.getString("ID");
			String creator=resultSet.getString("creator");
			Timestamp timestamp=resultSet.getTimestamp("timestamp");
			String description=resultSet.getString("description");
			String filename=resultSet.getString("filename");
			temp=new Doc(ID,creator,timestamp,description,filename);
			docs.put(ID, temp);
	    }
	    e=docs.elements();
	    return e;//返回枚举类
    }
	
	 public static synchronized boolean insertDoc(String ID,String creator,Timestamp timestamp,String description,String filename)throws SQLException{//在数据库中添加文件的方法
		 if(!connectedToDB)
			 throw new SQLException("Not Connected to Database.");
		 statement=connection.createStatement(
				 ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				 ResultSet.CONCUR_UPDATABLE);//可以用结果集更新数据库中的表
		 String sql="select * from doc_info where Id='"+ID+"'";
		 resultSet=statement.executeQuery(sql);
	     if(resultSet.next())
	    	 return false;
	     sql="insert into doc_info(Id,creator,timestamp,description,filename) values "+"('"+ID+"','"+creator+"','"+timestamp+"','"+description+"','"+filename+"')";
	     if(statement.executeUpdate(sql)>0)
	    	 return true;
	     else
	    	 return false;//返回布尔值判断是否添加成功
	 }
	 
	 public static User searchUser(String name) throws SQLException{//在数据库中查找指定用户名的方法,返回User类
		 User temp=null;
		 if(!connectedToDB)
			 throw new SQLException("Not Connected to Database.");
		 statement=connection.createStatement(
				 ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				 ResultSet.CONCUR_READ_ONLY);//不能用结果集更新数据库中的表
		 String sql="select * from user_info where username ='"+name+"'";
		 resultSet=statement.executeQuery(sql);
		 if(resultSet.next()) {
			String Name=resultSet.getString("username");
			String password=resultSet.getString("password");
			String role=resultSet.getString("role");
			temp=new User(Name,password,role);
		 }	
		 return temp;//返回User类
	 }
	 
	 public static User searchUser(String name,String password) throws SQLException{//重载上面的方法,实现密码判断,不做赘述
		 User temp=null;
		 if(!connectedToDB)
			 throw new SQLException("Not Connected to Database.");
		 statement=connection.createStatement(
				 ResultSet.TYPE_SCROLL_INSENSITIVE,
				 ResultSet.CONCUR_READ_ONLY);
		 String sql="select * from user_info where username='"+name+"'and password='"+password+"'";
		 resultSet=statement.executeQuery(sql);
		 if(resultSet.next()) {
			String Name=resultSet.getString("username");
			String Password=resultSet.getString("password");
			String role=resultSet.getString("role");
			temp=new User(Name,Password,role);
		 }
		 return temp;
	 }
	 
	 public static Enumeration<User> getAllUser() throws SQLException{//在数据库中获取所有文件的方法,返回User枚举类
		 Hashtable<String,User> users=new Hashtable<String,User>();
		 User temp = null;
		 Enumeration<User> e;
		 if(!connectedToDB)
			 throw new SQLException("Not Connected to Database.");
		 statement=connection.createStatement(
				 ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				 ResultSet.CONCUR_READ_ONLY);//不能用结果集更新数据库中的表
		 String sql="select * from user_info";
		 resultSet=statement.executeQuery(sql);
		 while(resultSet.next()) {
			String name=resultSet.getString("username");
			String password=resultSet.getString("password");
			String role=resultSet.getString("role");
			temp=new User(name,password,role);
			users.put(name, temp);
		 }
		 e=users.elements();
		 return e;//返回User的枚举类
	 }
	 
	 public static synchronized boolean updateUser(String name,String password,String role) throws SQLException{//在数据库中修改用户的方法
		 if(!connectedToDB)
			 throw new SQLException("Not Connected to Database.");
		 statement=connection.createStatement(
				 ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				 ResultSet.CONCUR_UPDATABLE);//可以用结果集更新数据库中的表
		 String sql="select * from user_info where username='"+name+"'";
		 resultSet=statement.executeQuery(sql);
		 if(!resultSet.next())
			 return false;
		 sql="update user_info set password='"+password+"',role='"+role+"' where username='"+name+"'";
		 if(statement.executeUpdate(sql)>0)
			 return true;
		 else
			 return false;//返回布尔值以判断是否修改成功
	 }
	 
	 public static synchronized boolean insertUser(String name,String password,String role) throws SQLException{//在数据库中新添用户的方法
		 if(!connectedToDB)
			 throw new SQLException("Not Connected to Database.");
		 statement=connection.createStatement(
				 ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				 ResultSet.CONCUR_UPDATABLE);//可以用结果集更新数据库中的表
		 String sql="select * from user_info where username='"+name+"'";
		 resultSet=statement.executeQuery(sql);
		 if(resultSet.next())
			 return false;
		 sql="insert into user_info(username,password,role) values "+"('"+name+"','"+password+"','"+role+"')";
		 if(statement.executeUpdate(sql)>0)
			 return true;
		 else
			 return false;//返回布尔值以判断是否修改成功
	 }
	 
	 public static synchronized boolean deleteUser(String name) throws SQLException{//在数据库中删除用户的方法
         if(!connectedToDB)
        	 throw new SQLException("Not Connected to Database.");
		 statement=connection.createStatement(
				 ResultSet.TYPE_SCROLL_INSENSITIVE,//结果集的游标可以上下移动,当数据库变化时,当前结果集不变
				 ResultSet.CONCUR_UPDATABLE);//可以用结果集更新数据库中的表
		 String sql="select * from user_info where username='"+name+"'";
		 resultSet=statement.executeQuery(sql);
		 if(!resultSet.next())
			 return false;
		 sql="delete from user_info where username='"+name+"'";
		 if(statement.executeUpdate(sql)>0)
			 return true;
		 else
			 return false;//返回布尔值以判断是否删除成功
	 }
}

Doc.java

package file_system;

import java.sql.*;

//文件类,封装文件的信息
class Doc{
	private String ID;
	private String creator;
	private Timestamp timestamp;
	private String description;
	private String filename;
	
	public Doc(String ID, String creator, Timestamp timestamp, String description, String filename) {
		super();
		this.ID = ID;
		this.creator = creator;
		this.timestamp = timestamp;
		this.description = description;
		this.filename=filename;
	}
	
	public String getID() {
		return ID;
	}
	public void setID(String iD) {
		ID = iD;
	}
	public String getCreator() {
		return creator;
	}
	public void setCreator(String creator) {
		this.creator = creator;
	}
	public Timestamp getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(Timestamp timestamp) {
		this.timestamp = timestamp;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
}

LoginWindow.java

package file_system;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

@SuppressWarnings("serial")
//登录界面
public class LoginWindow extends JFrame{
	JTabbedPane tabbedPane;
	public void loginFrame() {
		JFrame frame=new JFrame();
		frame.setTitle("系统登录");
		frame.setSize(400,300);
		frame.setLocationRelativeTo(null);//使窗口显示在屏幕中央
		frame.setResizable(false);//窗口不可改变大小
		frame.setLayout(null);//清空布局管理器
		JLabel L1=new JLabel("账号:");
		JTextField te1=new JTextField(20);
		JLabel L2=new JLabel("密码:");
		JPasswordField te2=new JPasswordField(20);
		te2.setEchoChar('*');//将输入的密码显示为*
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setBounds(30,50,50,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setBounds(80,50,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setBounds(30,100,50,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setBounds(80,100,250,30);
		frame.add(L1);
		frame.add(te1);
		frame.add(L2);
		frame.add(te2);
		JButton B1=new JButton("登录");
		B1.setFont(new Font("黑体",Font.PLAIN,18));
		B1.setBounds(80,170,80,50);
		JButton B2=new JButton("退出");
		B2.setFont(new Font("黑体",Font.PLAIN,18));
		B2.setBounds(250,170,80,50);
		frame.add(B1);
		frame.add(B2);
		B1.addActionListener(new ButtonHandler(frame,te1,te2));//对按钮监听,下同
		B2.addActionListener(new ButtonHandler(frame));
		frame.addWindowListener(new WindowListener() {//添加Windows监听,当点击右上角关闭时实现操作
			public void windowOpened(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
			public void windowClosing(WindowEvent e) {//点击关闭时向服务端发送取消登录的信息
				Client.sendData("Cancel login");
			}
			public void windowClosed(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
		});
		frame.setVisible(true);//界面设置为可见
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//单击窗口的关闭按钮时程序执行的操作
	}
	public class ButtonHandler implements ActionListener{
		public JTextField te1=new JTextField();
	    public JPasswordField te2=new JPasswordField();
	    public JFrame frame=new JFrame();
	    ButtonHandler(JFrame frame){
	    	this.frame=frame;
	    }
	    ButtonHandler(JFrame frame,JTextField te1,JPasswordField te2) {
	    	this.frame=frame;
			this.te1=te1;
			this.te2=te2;
		}
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("登录")) {
				String name=te1.getText();
				String password=String.valueOf(te2.getPassword());
				try {
					if(DataProcessing.searchUser(name)!=null) {//在数据库中查找用户名
						if(DataProcessing.searchUser(name ,password)!=null) {//检查密码是否正确
							frame.dispose();//销毁登录界面
							Client.sendData(name+" login in");//向服务端发送登录信息
							MenuWindow menuWindow=new MenuWindow();
							menuWindow.showMenu(name);//显示主菜单界面
							addWindowListener(new WindowAdapter() {
								@SuppressWarnings("unused")
								public void WindowClosing(WindowEvent e2) {
									DataProcessing.disconnectFromDB();//断开连接
								}
							});
						}
						else {
							JOptionPane.showMessageDialog(null, "密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
						}
					}
					else {
						JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
				} catch (HeadlessException e1) {
					JOptionPane.showMessageDialog(null,e1.getMessage(), "本地的显示设备,键盘,鼠标等不支持调用", JOptionPane.ERROR_MESSAGE);
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null,e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
					}
			}else if(e.getActionCommand().equals("退出")) {
				frame.dispose();//点击退出时销毁界面
				Client.sendData("Cancel login");//向服务端发送取消登录的消息
			}	
		}
	}
}

MenuWindow.java

package file_system;

import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

//菜单界面
public class MenuWindow {
    public void showMenu(String name) {
    	JFrame frame=new JFrame();
		frame.setTitle("菜单界面");
		frame.setSize(600,450);
		frame.setLocationRelativeTo(null);//使窗口显示在屏幕中央
		frame.setResizable(false);//窗口不可改变大小
		frame.setLayout(null);//清空布局管理器
		JMenuBar menu=new JMenuBar();//添加菜单栏
		JMenu menu1=new JMenu("用户管理");
		JMenu menu2=new JMenu("文件管理");
		JMenu menu3=new JMenu("密码管理");
		JMenuItem item1=new JMenuItem("添加用户");
		JMenuItem item2=new JMenuItem("修改用户");
		JMenuItem item3=new JMenuItem("删除用户");
		JMenuItem item4=new JMenuItem("上传文件");
		JMenuItem item5=new JMenuItem("下载文件");
		JMenuItem item6=new JMenuItem("修改密码");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//单击窗口的关闭按钮时程序执行的操作
		
		String role = null;
		try {
			role=DataProcessing.searchUser(name).getRole();//从数据库中获取该用户的角色
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);	
			}
		if(role.equals("administrator")) {//administrator不可上传文件
			item4.setEnabled(false);
		}
		else if(role.equals("operator")){//operator不可对用户进行操作
			menu1.setEnabled(false);
			item1.setEnabled(false);
			item2.setEnabled(false);
			item3.setEnabled(false);
		}
		else if(role.equals("browser")){//browser不可对用户进行操作和上传文件
			menu1.setEnabled(false);
			item1.setEnabled(false);
			item2.setEnabled(false);
			item3.setEnabled(false);
			item4.setEnabled(false);
		}
		menu1.add(item1);
		menu1.add(item2);
		menu1.add(item3);
		menu2.add(item4);
		menu2.add(item5);
		menu3.add(item6);
		menu.add(menu1);
		menu.add(menu2);
		menu.add(menu3);
		menu.setBounds(0,0,195,30);
		menu.setVisible(true);//菜单栏可见
		frame.add(menu);
		item1.addActionListener(new MenuAction(name));//添加监听,下同
		item2.addActionListener(new MenuAction(name));
		item3.addActionListener(new MenuAction(name));
		item4.addActionListener(new MenuAction(name));
		item5.addActionListener(new MenuAction(name));
		item6.addActionListener(new MenuAction(name));
		frame.setVisible(true);//界面可见
		frame.addWindowListener(new WindowListener() {//添加Windows监听,当点击右上角关闭时实现操作
			public void windowOpened(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
			public void windowClosing(WindowEvent e) {//点击关闭时向服务端发送信息
				Client.sendData(name + " logout");
			}
			public void windowClosed(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
		});
    }
    class MenuAction implements ActionListener{
    	String name;
    	MenuAction(String name){
    		this.name=name;
    	}
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("添加用户")) {
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(name,0);//下拉框的默认选择项为下拉框模型中的第1个变量,即添加用户
			}
			else if(e.getActionCommand().equals("修改用户")) {
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(name,1);//下拉框的默认选择项为下拉框模型中的第2个变量,即修改用户
			}
			else if(e.getActionCommand().equals("删除用户")) {
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(name,2);//下拉框的默认选择项为下拉框模型中的第3个变量,即删除用户
			}
			else if(e.getActionCommand().equals("上传文件")) {
				UpDownloadWindow updownloadWindow=new UpDownloadWindow();
				updownloadWindow.showMenu(name,1);//下拉框的默认选择项为下拉框模型中的第1个变量,即上传文件
			}
			else if(e.getActionCommand().equals("下载文件")) {
				UpDownloadWindow updownloadWindow=new UpDownloadWindow();
				updownloadWindow.showMenu(name,0);//下拉框的默认选择项为下拉模型中的第2个变量,即下载文件
			}
			else if(e.getActionCommand().equals("修改密码")) {
				PasswordWindow passwordWindow=new PasswordWindow();
				passwordWindow.showMenu(name);
			}
		}
    }
}

PasswordWindow.java

package file_system;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
//密码修改界面
public class PasswordWindow {
    String user_name;
    String user_role;
    public void showMenu(String name) {
        user_name=name;
        try {
			user_role=DataProcessing.searchUser(user_name).getRole();//获取当前登录用户的角色
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
		}
        JFrame frame=new JFrame();
		frame.setTitle("密码修改界面");
		frame.setSize(400,390);
		frame.setLocationRelativeTo(null);//使窗口显示在屏幕中央
		frame.setResizable(false);//设置窗口不可改变大小
		frame.setLayout(null);//清空布局管理器
		
		JLabel L1=new JLabel("账号:");
		JTextField te1=new JTextField(20);
		JLabel L2=new JLabel("旧密码:");
		JPasswordField te2=new JPasswordField(20);
		te2.setEchoChar('*');//将输入的密码显示为*,下同
		JLabel L3=new JLabel("新密码:");
		JPasswordField te3=new JPasswordField(20);
		te3.setEchoChar('*');
		JLabel L4=new JLabel("重复密码:");
		JPasswordField te4=new JPasswordField(20);
		te4.setEchoChar('*');
		JLabel L5=new JLabel("角色:");
		JTextField te5=new JTextField(20);
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setBounds(15,30,100,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setBounds(115,30,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setBounds(15,80,100,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setBounds(115,80,250,30);
		L3.setFont(new Font("黑体",Font.PLAIN,18));
		L3.setBounds(15,130,100,30);
		te3.setFont(new Font("黑体",Font.PLAIN,18));
		te3.setBounds(115,130,250,30);
		L4.setFont(new Font("黑体",Font.PLAIN,18));
		L4.setBounds(15,180,100,30);
		te4.setFont(new Font("黑体",Font.PLAIN,18));
		te4.setBounds(115,180,250,30);
		L5.setFont(new Font("黑体",Font.PLAIN,18));
		L5.setBounds(15,230,100,30);
		te5.setFont(new Font("黑体",Font.PLAIN,18));;
		te5.setBounds(115,230,250,30);
		frame.add(L1);
		frame.add(L2);
		frame.add(L3);
		frame.add(L4);
		frame.add(L5);
		frame.add(te1);
		frame.add(te2);
		frame.add(te3);
		frame.add(te4);
		frame.add(te5);
		te1.setText(user_name);
		te1.setEnabled(false);//用户名不可更改
		te5.setText(user_role);
		te5.setEnabled(false);//用户角色不可更改
		JButton B1=new JButton("确认");
		JButton B2=new JButton("退出");
		B1.setBounds(60,280,80,40);
		B2.setBounds(240,280,80,40);
		B1.addActionListener(new ButtonHandler(frame,te2,te3,te4));//对按钮添加监听,下同
		B2.addActionListener(new ButtonHandler(frame,te2,te3,te4));
		frame.add(B1);
		frame.add(B2);
		frame.setVisible(true);//设置界面可见
    }
    public class ButtonHandler implements ActionListener{//设置按钮监听
    	JFrame frame;
    	JTextField te2;
    	JTextField te3;
    	JTextField te4;
    	ButtonHandler(JFrame frame,JTextField te2,JTextField te3,JTextField te4){
    		this.frame=frame;
    		this.te2=te2;
    		this.te3=te3;
    		this.te4=te4;
    	}
		public void actionPerformed(ActionEvent e) {
			String old_password=te2.getText();
			String new_password1=te3.getText();
			String new_password2=te4.getText();
			if(e.getActionCommand().equals("确认")) {
				try {
					if(DataProcessing.searchUser(user_name, old_password)!=null) {//判断旧密码是否正确
						if(new_password1.equals(new_password2)) {//判断两次输入的密码是否一致
							DataProcessing.updateUser(user_name, new_password1, user_role);//调用修改数据库中用户的方法以实现修改密码
							frame.dispose();//销毁界面
							JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);//提示修改密码成功
							Client.sendData("Change self password");//向服务端发送修改密码的消息
						}
						else {
							JOptionPane.showMessageDialog(null, "两次输入的新密码不一致", "温馨提示", JOptionPane.ERROR_MESSAGE);
						}
					}
					else {
						JOptionPane.showMessageDialog(null, "旧密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
				}
			}else if(e.getActionCommand().equals("退出")) {
				frame.dispose();//销毁界面
			}
		}	
    }
}

Server.java

package file_system;

import java.io.*;
import java.net.*;

public class Server {
	private ServerSocket server; // 服务端套接字
	private Socket connection; // 连接到客户端
	private int counter = 1; // 客户端连接数计数
	private String servername = "";

	public void runServer() {
		try
		{
			server = new ServerSocket(12345, 100); // 创建服务端套接字
			while (true) {
				try {
					waitForConnection(); // 等待连接
				} 
				catch (EOFException eofException) {
					System.out.println("\nServer terminated connection");
				}
			}
		}
		catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	// 等待客户端连接,然后显示连接信息
	private void waitForConnection() throws IOException {
		System.out.println("Waiting for connection\n");
		connection = server.accept(); // 允许服务端接受连接
		servername = "Thread" + counter;
		counter++;
		System.out.println(
				"\n"+servername + ":" + servername + " received from: " + connection.getInetAddress().getHostAddress()+"\n");
		SocketServer thread = new SocketServer(connection,servername);//创建新线程
		thread.close();
	}

	public class SocketServer extends ServerSocket {
		public SocketServer(Socket socket,String name) throws IOException {
			new CreateServerThread(socket,name);//创建新线程
		}
	}

	// 线程类
	class CreateServerThread extends Thread {
		private ObjectOutputStream output; //发送到客户端的输出流
		private ObjectInputStream input; // 从客户端接受到的输入流
		private Socket client;

		public CreateServerThread(Socket s,String number) throws IOException {
			client = s;
			setName(number);
			start();
		}

		// 与客户端的进程连接
		private void processConnection() throws IOException {
			String message = "Connection successful";
			sendData(message); // 向客户端发送连接成功消息
			do
			{
				try
				{
					message = (String) input.readObject(); // 获取新消息
					System.out.println("\n" +getName()+"\n" + message); // 显示消息
				}
				catch (ClassNotFoundException classNotFoundException) {
					System.out.println("\nUnknown object type received");
				}catch (IOException e) {
				}

			} while (!message.equals("CLIENT>>> TERMINATE"));
		}

		// 向客户端发送消息
		private void sendData(String message) {
			try
			{
				output.writeObject("SERVER>>> " + message);
				output.flush(); // 刷新输出流到客户端
				System.out.println("\nSERVER>>> " + message);
			} 
			catch (IOException ioException) {
				System.out.println("\nError writing object");
			}
		}

		// 获取发送和接收数据的流
		private void getStreams() throws IOException {
			// 将对象设置输出流
			output = new ObjectOutputStream(client.getOutputStream());
			output.flush(); // 刷新输出流缓冲区以发送信息
			// 将对象设置输入流
			input = new ObjectInputStream(client.getInputStream());
			System.out.println("\nGot I/O streams\n");
		}
		
		// 关闭流和连接
		private void closeConnection() {
			System.out.println("\n"+getName()+"\nTerminating connection\n");
			try {
					output.close(); //关闭输出流
					input.close(); //关闭输入流
					connection.close(); // 关闭套接字
					client.close();
			}
			catch (IOException ioException) {
				ioException.printStackTrace();
			} 
		}
		
		public void run() {
			try {
				getStreams();// 获取输入和输出流
				processConnection(); //进行连接
				closeConnection();//关闭连接
			} catch (IOException e) {
			}
		}
	}

	public static void main(String args[]) {
		Server application = new Server(); // 创建服务端
		application.runServer(); // 运行服务端
	}
}

UpDownloadWindow.java

package file_system;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
//文件管理界面
public class UpDownloadWindow {
	String user_name;
    String uploadpath="D:\\OOP\\uploadfile\\";//上传文件的保存路径
    String downloadpath="D:\\OOP\\downloadfile\\";//下载文件的保存路径
    public void showMenu(String name,int index) {// index 为后面下拉框的默认选择项
    	user_name=name;
    	JFrame frame=new JFrame();
        frame.setTitle("文件管理界面");
		frame.setSize(480,360);
		frame.setLocationRelativeTo(null);//使窗口显示在屏幕中央
		frame.setResizable(false);//设置窗口不可改变大小
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);//设置此选项卡窗格的选项卡布局
		JPanel panel_download=new JPanel();//下载文件面板
		panel_download.setLayout(null);//清空布局管理器
		tabbedPane.addTab("文件下载", panel_download);
		String[] columnName={"ID","Creator","Time","FileName","Description"};
		String[][] rowData=new String[50][5];
		Enumeration<Doc> e=null;//创建枚举类型以存放文件信息
		try {
			e = DataProcessing.getAllDocs();//从数据库中获取所有文件信息存放在枚举类型e中
		} 
		catch (SQLException e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);	
			}
		Doc doc;
		String[] nameData=new String[50];
		int i=0;
		while(e.hasMoreElements()) {
			doc=e.nextElement();
			nameData[i]=rowData[i][0]=doc.getID();
			rowData[i][1]=doc.getCreator();
			rowData[i][2]=doc.getTimestamp().toString();
			rowData[i][3]=doc.getFilename();
			rowData[i][4]=doc.getDescription();
			i++;
		}
		@SuppressWarnings("serial")
		JTable table=new JTable(rowData,columnName) {//创建单元格以存放文件信息
			public boolean isCellEditable(int rowIndex,int ColIndex) {
				return false;//单元格内容不可被编辑
			}
		};
		table.setFont(new Font("黑体",Font.PLAIN,18));
		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scroll=new JScrollPane(table);
		scroll.setVisible(true);
		scroll.setBounds(0,0,460,160);
		panel_download.add(scroll);
		JButton B1=new JButton("下载");
		JButton B2=new JButton("退出");
		B1.setBounds(100,200,80,40);
		B2.setBounds(280,200,80,40);
		B1.addActionListener(new ButtonHandler(frame,table));//对按钮添加监听,下同
		B2.addActionListener(new ButtonHandler(frame));
		panel_download.add(B1);
		panel_download.add(B2);
		
		JPanel panel_upload=new JPanel();//上传文件面板
		panel_upload.setLayout(null);////清空布局管理器
		tabbedPane.addTab("文件上传", panel_upload);
		JLabel L1=new JLabel("档案号:");
		JTextField te1=new JTextField(20);
		JLabel L2=new JLabel("档案描述:");
		JTextArea te2=new JTextArea(6,20);
		JLabel L3=new JLabel("档案文件名:");
		JTextField te3=new JTextField(25);
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setBounds(5,0,100,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setBounds(105,0,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setBounds(5,50,100,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setBounds(105,50,250,100);
		L3.setFont(new Font("黑体",Font.PLAIN,18));
		L3.setBounds(5,180,100,30);
		te3.setFont(new Font("黑体",Font.PLAIN,18));
		te3.setBounds(105,180,250,30);
		panel_upload.add(L1);
		panel_upload.add(L2);
		panel_upload.add(L3);
		panel_upload.add(te1);
		panel_upload.add(te2);
		panel_upload.add(te3);
		JButton B3=new JButton("打开");
		JButton B4=new JButton("上传");
		JButton B5=new JButton("取消");
		B3.setBounds(360,180,60,30);
		B4.setBounds(105,230,80,40);
		B5.setBounds(275,230,80,40);
		B3.addActionListener(new ButtonHandler(te3));//对按钮添加监听,下同
		B4.addActionListener(new ButtonHandler(frame,te1,te2,te3));
		B5.addActionListener(new ButtonHandler(frame));
		panel_upload.add(B3);
		panel_upload.add(B4);
		panel_upload.add(B5);
		tabbedPane.setVisible(true);
		tabbedPane.setSelectedIndex(index);// index为下拉框的默认选择项
		try {
			if(DataProcessing.searchUser(user_name).getRole().equals("operator")) {//检查角色是否为operator,是可上传文件,反之则不行
				tabbedPane.setEnabledAt(1, true);
			}
			else tabbedPane.setEnabledAt(1, false);
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
		}
		frame.add(tabbedPane);
        frame.setVisible(true);//设置界面可见
    }
    public class ButtonHandler implements ActionListener{//设置按钮监听
    	JTextField te1;
    	JTextArea te2;
    	JTextField te3;
    	JFrame frame;
		JTable table=new JTable();
    	ButtonHandler(JTextField te3){
    		this.te3=te3;
    	}
    	ButtonHandler(JFrame frame,JTextField te1,JTextArea te2,JTextField te3){
    		this.frame=frame;
    		this.te1=te1;
    		this.te2=te2;
    		this.te3=te3;
    	}
    	ButtonHandler(JFrame frame){
    		this.frame=frame;
    	}
		ButtonHandler(JFrame frame,JTable table) {
	    	this.frame=frame;
			this.table=table;
		}
    	public void actionPerformed(ActionEvent e) {
    		boolean succeedFlag = true;//操作成功标识
    		JFileChooser fileChooser=new JFileChooser();//使用自带的文件选择器
		    if(e.getActionCommand().equals("下载")) {
		    	int op=JOptionPane.showConfirmDialog(null, "确认下载", "温馨提示", JOptionPane.YES_NO_OPTION);//提示确认框
		    	if(op==JOptionPane.YES_OPTION) {
		    		if(table.getSelectedRow()<0) ; 
			    	else {
			    		String id=(String)table.getValueAt(table.getSelectedRow(), 0);
			    		byte[] buffer=new byte[1024];
			       	    Doc doc=null;
			       	    try {
			       	    	doc = DataProcessing.searchDoc(id);//从数据库中找到要下载的文件
			   	    	} 
			       	    catch (SQLException e1) {
			       	    	JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
			       	    	succeedFlag = false;
			      		}
			        	File tempFile=new File(uploadpath+doc.getFilename());
			        	String filename=tempFile.getName();
			        	BufferedInputStream infile = null;
			        	BufferedOutputStream targetfile = null;
						try {
							infile = new BufferedInputStream(new FileInputStream(tempFile));
							targetfile = new BufferedOutputStream(new FileOutputStream(new File(downloadpath+filename)));
						} catch (FileNotFoundException e1) {
							JOptionPane.showMessageDialog(null, e1.getMessage(), "文件未找到", JOptionPane.ERROR_MESSAGE);
							succeedFlag = false;
						}
			        	while(true) {
			        		int byteRead = 0;
							try {
								byteRead = infile.read(buffer);
								if(byteRead==-1)
									break;
								targetfile.write(buffer,0,byteRead);
							}catch (IOException e1) {
								JOptionPane.showMessageDialog(null, e1.getMessage(), "文件访问错误", JOptionPane.ERROR_MESSAGE);
								succeedFlag = false;
							}
			        	 }
			       	     try {
							infile.close();
							targetfile.close();
						 } catch (IOException e1) {
							JOptionPane.showMessageDialog(null, e1.getMessage(), "关闭文件时发生错误", JOptionPane.ERROR_MESSAGE);
							succeedFlag = false;
						 }
			       	     if (succeedFlag) {
			        	 JOptionPane.showMessageDialog(null, "下载成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
			       	     Client.sendData("Download the file: "+filename);//向服务端发送下载文件的消息
			       	     }
			    	}
		    	}
		    }
		    else if(e.getActionCommand().equals("退出")) {
		    	frame.dispose();
		    }
		    else if(e.getActionCommand().equals("打开")) {
    			fileChooser.setFileSelectionMode(0);//使用文件选择器
    			int state=fileChooser.showOpenDialog(null);
    			if(state==1) {
    				return ;
    			}
    			else {
    				File file=fileChooser.getSelectedFile();
    				te3.setText(file.getAbsolutePath());
    			}
    		}
    		else if(e.getActionCommand().equals("上传")){
    			int op=JOptionPane.showConfirmDialog(null, "确认上传", "温馨提示", JOptionPane.YES_NO_OPTION);//提示确认框
    			if(op==JOptionPane.YES_OPTION) {
    				String ID=te1.getText();
        			String description=te2.getText();
        			String filename=te3.getText();
        			byte[] buffer=new byte[1024];
        	   	    Timestamp timestamp=new Timestamp(System.currentTimeMillis());//获取当前时间
        	   	    File tempFile=new File(filename.trim());
        	        String fileName=tempFile.getName();
        	   	    try {
        				if(DataProcessing.insertDoc(ID,user_name,timestamp,description,fileName)) ;//调用向数据库存存入文件的方法
        				else {
        					JOptionPane.showMessageDialog(null, "存入数据库失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
        					succeedFlag = false ; 
        				}
        			} 
        			catch (SQLException e1) {
        				JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
        				succeedFlag = false ; 
        			}
        	   	    BufferedInputStream infile = null;
        	   	    BufferedOutputStream targetfile = null;
        		    try {
    					infile = new BufferedInputStream(new FileInputStream(filename));
    					targetfile = new BufferedOutputStream(new FileOutputStream(new File(uploadpath+fileName)));
    				} catch (FileNotFoundException e1) {
    					JOptionPane.showMessageDialog(null, e1.getMessage(), "文件未找到", JOptionPane.ERROR_MESSAGE);
    					succeedFlag = false ; 
    				}
        	   	    while(true) {
        	   		    int byteRead = 0;
        			    try {
    						byteRead = infile.read(buffer);
    						if(byteRead==-1)
    							break;
    						targetfile.write(buffer,0,byteRead);
    					}catch (IOException e1) {
    						JOptionPane.showMessageDialog(null, e1.getMessage(), "文件访问错误", JOptionPane.ERROR_MESSAGE);
    						succeedFlag = false ; 
    					}
        	   	    }
        			try {
    					infile.close();
    					targetfile.close();
    				} catch (IOException e1) {
    					JOptionPane.showMessageDialog(null, e1.getMessage(), "关闭文件时发生错误", JOptionPane.ERROR_MESSAGE);
    					succeedFlag = false ; 
    				}
        			if(succeedFlag) {
        			JOptionPane.showMessageDialog(null, "上传成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
        			Client.sendData("Upload the file "+filename);//向服务端发送上传文件的消息
        			}
        			frame.dispose();//销毁当前界面
    				UpDownloadWindow updownloadWindow=new UpDownloadWindow();
    				updownloadWindow.showMenu(user_name,1);//重新显示上传文件界面以达到刷新文件列表的效果
    			}else if(op==JOptionPane.NO_OPTION) 
    				;
    		}
    		else if(e.getActionCommand()=="取消") {
    			frame.dispose();//销毁界面
    		}
    	}
    }
}

Users.java

package file_system;
//用户类  封装用户的信息
public class User {
	private String name;
	private String password;
	private String role;
	
	User(String name,String password,String role){
		this.name=name;
		this.password=password;
		this.role=role;				
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
}

UserWindow.java

package file_system;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
//用户管理界面
public class UserWindow{
	String admin_name;//登录的管理员用户名
	public void showMenu(String name,int index) {// index 为后面下拉框的默认选择项
		admin_name=name;
		JFrame frame=new JFrame();
		frame.setTitle("用户管理界面");
		frame.setSize(400,300);
		frame.setLocationRelativeTo(null);//使窗口显示在屏幕中央
		frame.setResizable(false);//设置窗口不可改变大小
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);//设置此选项卡窗格的选项卡布局
		JPanel panel_add=new JPanel();//新添用户面板
		panel_add.setLayout(null);//清空布局管理器
		tabbedPane.addTab("添加用户", panel_add);
		JLabel L1=new JLabel("账号:");
		JTextField te1=new JTextField(20);
		JLabel L2=new JLabel("密码:");
		JPasswordField te2=new JPasswordField(20);
		JLabel L3=new JLabel("角色:");
		String role[]= {"administrator","operator","browser"};
		JComboBox<String> box=new JComboBox<String>(role);//创建下拉列表以选择要添加用户的角色
		te2.setEchoChar('*');
		L1.setFont(new Font("黑体",Font.PLAIN,18));
		L1.setBounds(35,30,50,30);
		te1.setFont(new Font("黑体",Font.PLAIN,18));
		te1.setBounds(85,30,250,30);
		L2.setFont(new Font("黑体",Font.PLAIN,18));
		L2.setBounds(35,80,50,30);
		te2.setFont(new Font("黑体",Font.PLAIN,18));
		te2.setBounds(85,80,250,30);
		L3.setFont(new Font("黑体",Font.PLAIN,18));
		L3.setBounds(35,130,50,30);
		box.setFont(new Font("黑体",Font.PLAIN,18));
		box.setBounds(85,130,200,30);
		panel_add.add(L1);
		panel_add.add(te1);
		panel_add.add(L2);
		panel_add.add(te2);
		panel_add.add(L3);
		panel_add.add(box);
		JButton B1=new JButton("添加");
		JButton B2=new JButton("退出");
		B1.setBounds(90,180,80,40);
		B2.setBounds(210,180,80,40);
		panel_add.add(B1);
		panel_add.add(B2);
		B1.addActionListener(new ButtonHandler(frame,te1,te2,box));//对按钮添加监听,下同
		B2.addActionListener(new ButtonHandler(frame));
	
		String[] columnName={"用户名","密码","角色"};
		String[][] rowData=new String[50][3];
		Enumeration<User> e=null;//创建枚举类型以存放用户信息
		try {
			e = DataProcessing.getAllUser();//从数据库中获取所有用户信息存放在枚举类型e中
		} 
		catch (SQLException e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
		}
		User user;
		String[] nameData=new String[50];
		int i=0;
		while(e.hasMoreElements()) {
			user=e.nextElement();
			nameData[i]=rowData[i][0]=user.getName();
			rowData[i][1]=user.getPassword();
			rowData[i][2]=user.getRole();
			i++;
		}
		
		JPanel panel_mod=new JPanel();//修改用户面板
		panel_mod.setLayout(null);//清空布局管理器
		tabbedPane.addTab("修改用户", panel_mod);
		JLabel L4=new JLabel("账号:");
		JComboBox<String> box_name=new JComboBox<String>(nameData);//创建下拉列表以选择要修改的用户
		JLabel L5=new JLabel("密码:");
		JPasswordField te4=new JPasswordField(20);
		JLabel L6=new JLabel("角色:");
		JComboBox<String> box2=new JComboBox<String>(role);//创建下拉列表以选择要修改的用户角色
		te4.setEchoChar('*');//将输入的密码显示为*
		L4.setFont(new Font("黑体",Font.PLAIN,18));
		L4.setBounds(35,30,50,30);
		box_name.setFont(new Font("黑体",Font.PLAIN,18));
		box_name.setBounds(85,30,250,30);
		L5.setFont(new Font("黑体",Font.PLAIN,18));
		L5.setBounds(35,80,50,30);
		te4.setFont(new Font("黑体",Font.PLAIN,18));
		te4.setBounds(85,80,250,30);
		L6.setFont(new Font("黑体",Font.PLAIN,18));
		L6.setBounds(35,130,50,30);
		box2.setFont(new Font("黑体",Font.PLAIN,18));
		box2.setBounds(85,130,200,30);
		panel_mod.add(L4);
		panel_mod.add(box_name);
		panel_mod.add(L5);
		panel_mod.add(te4);
		panel_mod.add(L6);
		panel_mod.add(box2);
		JButton B3=new JButton("修改");
		JButton B4=new JButton("退出");
		B3.setBounds(90,180,80,40);
		B4.setBounds(210,180,80,40);
		panel_mod.add(B3);
		panel_mod.add(B4);
		B3.addActionListener(new ButtonHandler(frame,box_name,te4,box2));//对按钮添加监听,下同
		B4.addActionListener(new ButtonHandler(frame));
		
		JPanel panel_del=new JPanel();//删除用户面板
		panel_del.setLayout(null);//清空布局管理器
		tabbedPane.addTab("删除用户", panel_del);
		@SuppressWarnings("serial")
		JTable table=new JTable(rowData,columnName) {//创建单元格以存放用户信息
			public boolean isCellEditable(int rowIndex,int ColIndex) {
				return false;//单元格内容不可被编辑
			}
		};
		table.setFont(new Font("黑体",Font.PLAIN,18));
		table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//设置单个单元格选择
		JScrollPane scroll=new JScrollPane(table);
		scroll.setVisible(true);//设置表格可见
		scroll.setBounds(0,0,380,160);
		panel_del.add(scroll);
		JButton B5=new JButton("删除");
		JButton B6=new JButton("退出");
		B5.setBounds(90,180,80,40);
		B6.setBounds(210,180,80,40);
		panel_del.add(B5);
		panel_del.add(B6);
		B5.addActionListener(new ButtonHandler(frame,table));
		B6.addActionListener(new ButtonHandler(frame));
		tabbedPane.setVisible(true);
		tabbedPane.setSelectedIndex(index);// index为下拉框的默认选择项
		frame.add(tabbedPane);
		frame.setVisible(true);//设置界面可见
	}
	public class ButtonHandler implements ActionListener{//设置按钮监听
		public JTextField te1=new JTextField();
	    public JPasswordField te2=new JPasswordField();
	    public JFrame frame=new JFrame();
	    public JComboBox<String> box=new JComboBox<String>();
	    public JComboBox<String> box_name=new JComboBox<String>();
	    public JTable table;
	    public String role;
	    ButtonHandler(JFrame frame){
	    	this.frame=frame;
	    }
	    ButtonHandler(JFrame frame,JTextField te1,JPasswordField te2,JComboBox<String> box){
	    	this.frame=frame;
			this.te1=te1;
			this.te2=te2;
			this.box=box;
	    }
	    ButtonHandler(JFrame frame,JComboBox<String> box_name,JPasswordField te2,JComboBox<String> box) {
	    	this.frame=frame;
			this.box_name=box_name;
			this.te2=te2;
			this.box=box;
		}
	    ButtonHandler(JFrame frame,JTable table) {
	    	this.frame=frame;
			this.table=table;
		}
		public void actionPerformed(ActionEvent e) {
			String name=te1.getText();
			String password=String.valueOf(te2.getPassword());
			if(e.getActionCommand().equals("修改")) {
				try {
					name=(String)box_name.getSelectedItem();
					if(DataProcessing.searchUser(name)!=null) {
						role=(String)box.getSelectedItem();
						DataProcessing.updateUser(name, password, role);//调用修改数据库中用户的方法
						JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
						Client.sendData("Change "+name+"'s info");//向服务端发送修改用户的消息
						frame.dispose();//销毁当前界面
						UserWindow userWindow=new UserWindow();
						userWindow.showMenu(admin_name,1);//重新显示修改用户界面以达到刷新效果
					}
					else {
						JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
				} catch (HeadlessException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), "本地的显示设备,键盘,鼠标等不支持调用", JOptionPane.ERROR_MESSAGE);
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(e.getActionCommand().equals("添加")){
				role=(String)box.getSelectedItem();
				try {
					if (DataProcessing.insertUser(name, password, role)) {//调用向数据库中添加用户的方法
						JOptionPane.showMessageDialog(null, "添加成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
						Client.sendData("Insert user "+name);//向服务端发送添加用户的消息
					}else {
						JOptionPane.showMessageDialog(null, "添加失败", "温馨提示", JOptionPane.PLAIN_MESSAGE);
					}
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
				}
				frame.dispose();//销毁当前界面
				UserWindow userWindow=new UserWindow();
				userWindow.showMenu(admin_name,0);//重新显示添加用户界面以达到刷新效果
			}	
			else if(e.getActionCommand().equals("删除")){
				if(table.getSelectedRow()<0) ; 
				else {
					String name_del=(String) table.getValueAt(table.getSelectedRow(), 0);
					if(name_del.equals(admin_name)) {//检查将要删除的用户是否为登录者自身
						JOptionPane.showMessageDialog(null, "无法删除自己", "温馨提示", JOptionPane.ERROR_MESSAGE);
					}
					else {
						try {
							if(DataProcessing.deleteUser(name_del)) {//调用删除数据库中用户的方法
								JOptionPane.showMessageDialog(null, "删除成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
								Client.sendData("Delete user "+name_del);//向服务端发送删除用户的消息
							}else {
								JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
							}
						} catch (SQLException e1) {
							JOptionPane.showMessageDialog(null, e1.getMessage(), "数据库错误", JOptionPane.ERROR_MESSAGE);
						}
						frame.dispose();//销毁当前界面
						UserWindow userWindow=new UserWindow();
						userWindow.showMenu(admin_name,2);//重新显示删除用户界面以达到刷新用户表格的效果
					}
				}
			}else if(e.getActionCommand().equals("退出")) {
				frame.dispose();//点击退出时销毁当前界面
			}
		}
	}
}

你可能感兴趣的:(【归档】档案管理系统(java语言))