【归档】档案管理系统
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