第四次验收:面向对象与多线程综合实验之网络编程与多线程版本
面向对象与多线程综合实验是一次大项目,总共分为4个部分进行验收,我将分成四个部分展示4个版本的项目工程。希望看到本文章的你,对你有所收获。
文章目录
档案管理系统简介
档案管理系统:用于规范档案文件管理,构建档案资源信息共享服务平台,为用户提供完整的档案管理和网络查询功能。
• 系统是一个基于C/S的GUI应用程序
• 使用Java SE开发
• 综合运用面向对象编程的相关知识
系统环境
系统开发环境:JavaSE-12
集成开发工具:Eclipse Java 2019-06
GUI开发插件:Window Builder
数据库:MySQL Server 8.0.22+Navicat Premium 12
系统功能
基于TCP的Java Socket连接过程
建立连接的过程
• 服务器端生成一个ServerSocket实例对象,随时监听客户端的连接请求
• 客户端生成一个Socket实例对象,并发出连接请求
• 服务器端通过accept()方法接收到客户端的请求后,新建一个Socket与之进行连接
• 通信都是通过一对InputStream和OutputStream进行的
• 通信结束后,两端分别关闭对应的Socket
基于TCP的Socket编程
• 由Socket获得的输入流与输出流,可设为ObjectInputStream和
ObjectOutputStream
• 需要设计客户端与服务器端的通信规则,让通信双方明白将要发送的是什么。例如收到>>>CLIENT_FILE_UP,表明客户端要上传档案文件
• 可设计相应的类传送文件的相关属性信息,该类必须可序列化
• 客户端可以直连数据库;也可将所有请求都提交服务器,由服务器连接
数据库,处理相关操作,把结果返还给客户端
多线程Socket编程
解决方案
• 引入多线程
• 将与Client通信的任务为派给另一个线程
以下是本篇文章正文内容,下面案例可供参考
具体实现
1.服务器端
Server.java
数据库加载放在服务器端。
代码如下(示例):
package Internet1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Enumeration;
import javax.swing.JTextArea;
import MSystem.DataProcessing;
import MSystem.Doc;
import MSystem.User;
public class Server extends ServerSocket
{
private static JTextArea displayArea; // display information to user
private static ObjectOutputStream output; // output stream to client
private static ObjectInputStream input; // input stream from client
private static Socket connection; // connection to client
String uploadpath="..\\ManagementSystem-v4(Server)\\uploadfile\\";
Timestamp timestamp=new Timestamp(System.currentTimeMillis());
// set up GUI
private static final int SERVER_PORT =12345;
public Server()throws IOException, SQLException
{
// super( "Server" );
super(SERVER_PORT);
try {
while (true) {
Socket socket = accept();
System.out.println("Connection "+ " received from: "+socket.getInetAddress().getHostName() );
CreateServerThread a=new CreateServerThread();
a. ServerThread(socket);//当有请求时,启一个线程处理
}
}catch (IOException e) {
}finally {
close();
}
}
/* enterField = new JTextField(); // create enterField
enterField.setEditable( false );
enterField.addActionListener(
new ActionListener()
{
// send message to client
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
enterField.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
/* getContentPane().add( enterField, BorderLayout.NORTH );
displayArea = new JTextArea(); // create displayArea
getContentPane().add( new JScrollPane( displayArea ), BorderLayout.CENTER );
setSize( 300, 150 ); // set size of window
setVisible( true ); // show window
// end Server constructor*/
class CreateServerThread extends Thread {
//private Socket client;
/* private BufferedReader bufferedReader;
private PrintWriter printWriter;*/
public void ServerThread(Socket s)throws IOException {
connection = s;
output = new ObjectOutputStream(connection.getOutputStream());
input = new ObjectInputStream(connection.getInputStream());
output.flush();
//printWriter =new PrintWriter(client.getOutputStream(),true);
System.out.println("\nGot I/O streams\n");
System.out.println("Client(" + getName() +") come in...");
start();
}
// set up and run server
/* public void run()
{
try // set up server to receive connections; process connections
{
server = new ServerSocket( 12345, 100 ); // create ServerSocket
while ( true )
{
try
{
waitForConnection(); // wait for a connection
getStreams(); // get input & output streams
processConnection(); // process connection
} // end try
catch ( EOFException eofException )
{
displayMessage( "\nServer terminated connection" );
} // end catch
finally
{
closeConnection(); // close connection
counter++;
} // end finally
} // end while
} // end try
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
} // end method runServer
// wait for connection to arrive, then display connection info
private void waitForConnection() throws IOException
{
displayMessage( "Waiting for connection\n" );
connection = server.accept(); // allow server to accept connection
displayMessage( "Connection " + counter + " received from: " +
connection.getInetAddress().getHostName() );
} // end method waitForConnection
// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream( connection.getOutputStream() );
output.flush(); // flush output buffer to send header information
// set up input stream for objects
input = new ObjectInputStream( connection.getInputStream() );
displayMessage( "\nGot I/O streams\n" );
} // end method getStreams
// process connection with client*/
public void run()
{
String message = "Connection successful";
System.out.println( message ); // send connection successful message
// enable enterField so server user can send messages
// setTextFieldEditable( true );
do // process messages sent from client
{
try {
message = (String)input.readObject();
//displayMessage( "\n" + message ); // display message
System.out.println("Client(" + getName() +") say: " + message);
if(message.equals("CLIENT>>> CLIENT_LOGIN")) {
String name = null;
name = (String)input.readObject();
String password = null;
password = (String)input.readObject();
if(DataProcessing.search(name, password)!=null) {
sendData("CLIENT_LOGIN_TRUE");
String role=DataProcessing.getrole(name);
output.writeObject(role);
output.flush();
}
else {
sendData("CLIENT_LOGIN_FALSE");
}
} else if(message.equals("CLIENT>>> CLIENT_CHANGESELF")) {
String name=(String)input.readObject();
String password=(String)input.readObject();
String role=(String)input.readObject();
try {
if(DataProcessing.updateUser(name, password, role))
{
sendData("CLIENT_CHANGESELF_TRUE");
output.writeObject(password);
output.flush();
}
else sendData("CLIENT_CHANGESELF_FAlSE");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(message.equals("CLIENT>>> CLIENT_DELETEUSER")) {
String name=(String)input.readObject();
try {
if(DataProcessing.deleteUser(name))
sendData("CLIENT_DELETEUSER_TRUE");
else
sendData("CLIENT_DELETEUSER_FALSE");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(message.equals("CLIENT>>> CLIENT_INSERTUSER")) {
String name=(String)input.readObject();
String password=(String)input.readObject();
String role=(String)input.readObject();
try {
if(DataProcessing.insertUser(name, password, role))
sendData("CLIENT_INSERTUSER_TRUE");
else
sendData("CLIENT_INSERTUSER_FALSE");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(message.equals("CLIENT>>> CLIENT_UPDATEUSER")) {
String name=(String)input.readObject();
String password=(String)input.readObject();
String role=(String)input.readObject();
try {
if(DataProcessing.updateUser(name, password, role))
sendData("CLIENT_UPDATEUSER_TRUE");
else
sendData("CLIENT_UPDATEUSER_FALSE");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(message.equals("CLIENT>>> CLIENT_UPLOADFILE")) {
String ID=(String)input.readObject();
String creator=(String)input.readObject();
String description=(String)input.readObject();
String filename=(String)input.readObject();
Long filelength=input.readLong();
byte[] buffer=new byte[1024];
int l=0;
FileOutputStream out=new FileOutputStream(new File(uploadpath+filename));
System.out.println("开始接收文件<"+filename+">,文件大小为<"+filelength+">");
while(true) {
int read=input.read(buffer,0,buffer.length);
if(read==-1) break;
l+=read;
System.out.println("接收文件的进度"+100.0*l/filelength+"%...");
out.write(buffer,0,read);
out.flush();
if(l>=filelength)break;
}
out.close();
System.out.println("接收文件<"+filename+">成功");
try {
if(DataProcessing.insertDoc(ID, creator, timestamp, description, filename))
sendData("CLIENT_UPLOADFILE_TRUE");
else
sendData("CLIENT_UPLOADFILE_FALSE");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(message.equals("CLIENT>>> CLIENT_DOWNLOADFILE")) {
sendData("CLIENT_DOWNLOADFILEING");
String ID=(String)input.readObject();
String filename = DataProcessing.searchDoc(ID).getFilename();
File file=new File(uploadpath+filename);
long fileLength=file.length();
output.writeObject(filename);
output.flush();
output.writeLong(fileLength);
output.flush();
FileInputStream in=new FileInputStream(file);
byte[] buffer=new byte[1024];
int length=in.read(buffer,0,buffer.length);
while(length>0) {
output.write(buffer,0,length);
output.flush();
}
in.close();
}else if(message.equals("CLIENT>>> CLIENT_LISTUSER")) {
Enumeration<User> e=DataProcessing.getAllUser();
String[][] rowData=new String[50][3];
User user=null;
int x=0;
while(e.hasMoreElements()) {
user=e.nextElement();
rowData[x][0]=user.getName();
rowData[x][1]=user.getPassword();
rowData[x][2]=user.getRole();
x++;
}
sendData("CLIENT_LISTUSER");
output.writeInt(x);
output.flush();
for(int j=0;j<x;j++) {
for(int a=0;a<3;a++) {
output.writeObject(rowData[j][a]);
output.flush();
}
}
}
else if(message.equals("CLIENT>>> CLIENT_LISTDOC")) {
Enumeration<Doc> e=DataProcessing.getAllDocs();
String[][] rowData=new String[50][5];
Doc doc=null;
int i=0;
while(e.hasMoreElements()) {
doc=e.nextElement();
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++;
}
sendData("CLIENT_LISTDOC");
output.writeInt(i);
output.flush();
for(int j=0;j<i;j++) {
for(int i1=0;i1<=4;i1++) {
output.writeObject(rowData[j][i1]);
output.flush();
}
}
}
// end try
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} while ( !message.equals( "CLIENT>>> TERMINATE" ) );
System.out.println("Client(" + getName() +") exit!");
}
// close streams and socket
@SuppressWarnings("unused")
private void closeConnection()
{
//displayMessage( "\nTerminating connection\n" );
// setTextFieldEditable( false ); // disable enterField
try
{
output.close(); // close output stream
input.close(); // close input stream
connection.close(); // close socket
} // end try
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
} // end method closeConnection
// send message to client
private void sendData( String message )
{
try // send object to client
{
output.writeObject( "SERVER>>> " + message );
output.flush(); // flush output to client
System.out.println( "\nSERVER>>> "+"Client(" + getName() +"): " + message );
} // end try
catch ( IOException ioException )
{
displayArea.append( "\nError writing object" );
} // end catch
} // end method sendData
// manipulates displayArea in the event-dispatch thread
/*private void displayMessage( final String messageToDisplay )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run() // updates displayArea
{
displayArea.append( messageToDisplay ); // append message
} // end method run
} // end anonymous inner class
); // end call to SwingUtilities.invokeLater
} // end method displayMessage
// manipulates enterField in the event-dispatch thread
/* private void setTextFieldEditable( final boolean editable )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run() // sets enterField's editability
{
enterField.setEditable( editable );
} // end method run
} // end inner class
); // end call to SwingUtilities.invokeLater
} // end method setTextFieldEditable*/
}
@SuppressWarnings("resource")
public static void main( String args[] ){
String driverName="com.mysql.cj.jdbc.Driver"; // 加载数据库驱动类
String url="jdbc:mysql://localhost:3306/document?serverTimezone=GMT"; // 声明数据库的URL
String user="root"; // 数据库用户
String password="123456";
try {
DataProcessing.connectToDatabase(driverName, url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Waiting for connection");
try {
new Server();
} catch (IOException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
new Server();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// end class Server
2.客户端
通过更改IP地址来实现连接不同的服务器。
StartI.java
package Internet;
import MSystem.GUI.MainGUI;
public class StartI {
public static void main(String[] args) {
Client application; // declare client application
MainGUI.main(null);
if(args.length==0)
application=new Client("127.0.0.1");
else
application=new Client( args[ 0 ] );
application.runClient();
}
}
Client.java
package Internet;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import MSystem.GUI.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Client extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
private static JTextField enterField; // enters information from user
private static JTextArea displayArea; // display information to user
private static ObjectOutputStream output; // output stream to server
private static ObjectInputStream input; // input stream from server
private String message = ""; // message from server
private String chatServer; // host server for this application
private static Socket client; // socket to communicate with server
private static String user_role;
private static String user_password;
private static String user_name;
private static String[][] UserData;
public static int i=0;
public static int z=0;
private static String[][] DocData;
private static String filepath1;
// initialize chatServer and set up GUI
public Client( String host )
{
super( "Client" );
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
sendData("TERMINATE");
}
});
chatServer = host; // set server to which this client connects
enterField = new JTextField(); // create enterField
enterField.setEditable( false );
enterField.addActionListener(
new ActionListener()
{
// send message to server
public void actionPerformed( ActionEvent event )
{
sendData( event.getActionCommand() );
enterField.setText( "" );
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
getContentPane().add( enterField, BorderLayout.NORTH );
displayArea = new JTextArea(); // create displayArea
getContentPane().add( new JScrollPane( displayArea ), BorderLayout.CENTER );
setSize( 300, 150 ); // set size of window
setVisible( true ); // show window
} // end Client constructor
// connect to server and process messages from server
public void runClient()
{
try // connect to server, get streams, process connection
{
connectToServer(); // create a Socket to make connection
getStreams(); // get the input and output streams
processConnection(); // process connection
} // end try
catch ( EOFException eofException )
{
displayMessage( "\nClient terminated connection" );
} // end catch
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
finally
{
closeConnection(); // close connection
} // end finally
} // end method runClient
// connect to server
private void connectToServer() throws IOException
{
displayMessage( "Attempting connection\n" );
// create Socket to make connection to server
client = new Socket( InetAddress.getByName( chatServer ), 12345);
// display connection information
displayMessage( "Connected to: " +
client.getInetAddress().getHostName() );
} // end method connectToServer
// get streams to send and receive data
private void getStreams() throws IOException
{
// set up output stream for objects
output = new ObjectOutputStream( client.getOutputStream() );
output.flush(); // flush output buffer to send header information
// set up input stream for objects
input = new ObjectInputStream( client.getInputStream() );
displayMessage( "\nGot I/O streams\n" );
} // end method getStreams
// process connection with server
private void processConnection() throws IOException
{
// enable enterField so client user can send messages
setTextFieldEditable( true );
do // process messages sent from server
{
try {
message = (String)input.readObject();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
displayMessage( "\n" + message ); // display message
if(message.equals("SERVER>>> CLIENT_LOGIN_TRUE")) {
try {
user_role = (String)input.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MenuGUI.main(null);
MainGUI.frame.dispose();
}
else if(message.equals("SERVER>>> CLIENT_LOGIN_FALSE")) {
JOptionPane.showMessageDialog(null, "账号或密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_CHANGESELF_TRUE")) {
JOptionPane.showMessageDialog(null, "修改密码成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
String password = null;
try {
password = (String)input.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
user_password=password;
System.out.println("SERVER>>> CLIENT_CHANGESELF_TRUE");
}
else if(message.equals("SERVER>>> CLIENT_CHANGESELF_FAlSE")) {
JOptionPane.showMessageDialog(null, "修改密码失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_LISTUSER")) {
UserData = new String[50][3];
i=input.readInt();
for(int j=0;j<i;j++) {
for(int a=0;a<3;a++) {
try {
UserData[j][a]=(String)input.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
else if(message.equals("SERVER>>> CLIENT_LISTDOC")) {
DocData = new String[50][5];
z=input.readInt();
for(int j=0;j<z;j++) {
for(int b=0;b<5;b++) {
try {
DocData[j][b]=(String)input.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
else if(message.equals("SERVER>>> CLIENT_DELETEUSER_TRUE")) {
JOptionPane.showMessageDialog(null, "删除用户成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_DELETEUSER_FALSE")) {
JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_INSERTUSER_TRUE")) {
JOptionPane.showMessageDialog(null, "添加用户成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_INSERTUSER_FALSE")) {
JOptionPane.showMessageDialog(null, "添加用户失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_UPDATEUSER_TRUE")) {
JOptionPane.showMessageDialog(null, "修改用户成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_UPDATEUSER_FALSE")) {
JOptionPane.showMessageDialog(null, "修改用户失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_UPLOADFILE_TRUE")) {
JOptionPane.showMessageDialog(null, "上传文件成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
}
else if(message.equals("SERVER>>> CLIENT_UPLOADFILE_FALSE")) {
JOptionPane.showMessageDialog(null, "上传文件失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
}else if(message.equals("SERVER>>> CLIENT_DOWNLOADFILEING")) {
String filename = null;
try {
filename = (String)input.readObject();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String filepath = filepath1;
Long fileLength=input.readLong();
FileOutputStream out=new FileOutputStream(new File(filepath+filename));
byte[] buffer=new byte[1024];
int l=0;
System.out.println("开始下载文件<"+filename+">,文件大小为<"+fileLength);
while(true) {
int read=input.read(buffer,0,buffer.length);
if(read==-1) break;
l+=read;
System.out.println("下载文件进度"+100.0*l/fileLength+"%...");
out.write(buffer,0,read);
out.flush();
if(l>=fileLength) break;
}
out.close();
System.out.println("下载文件<"+filename+">成功");
JOptionPane.showMessageDialog(null, "下载文件成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
} // end method processConnection
// close streams and socket
public static void closeConnection()
{
displayMessage( "\nClosing connection" );
setTextFieldEditable( false ); // disable enterField
String logout="CLIENET>>> CLIENT_LOGOUT";
try {
output.writeUTF(logout);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
output.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(logout);
try
{
output.close(); // close output stream
input.close(); // close input stream
client.close(); // close socket
} // end try
catch ( IOException ioException )
{
ioException.printStackTrace();
} // end catch
} // end method closeConnection
// send message to server
private static void sendData( String message )
{
try // send object to server
{
output.writeObject( "CLIENT>>> " + message );
output.flush(); // flush data to output
displayMessage( "\nCLIENT>>> " + message );
} // end try
catch ( IOException ioException )
{
displayArea.append( "\nError writing object" );
} // end catch
} // end method sendData
// manipulates displayArea in the event-dispatch thread
private static void displayMessage( final String messageToDisplay )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run() // updates displayArea
{
displayArea.append( messageToDisplay );
} // end method run
} // end anonymous inner class
); // end call to SwingUtilities.invokeLater
} // end method displayMessage
// manipulates enterField in the event-dispatch thread
private static void setTextFieldEditable( final boolean editable )
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run() // sets enterField's editability
{
enterField.setEditable( editable );
} // end method run
} // end anonymous inner class
); // end call to SwingUtilities.invokeLater
} // end method setTextFieldEditable
// end class Client
synchronized public static void Login(String name,String password) throws IOException {
sendData("CLIENT_LOGIN");
user_name=name;
user_password=password;
output.writeObject(name);
output.flush();
output.writeObject(password);
output.flush();
}
synchronized public static void ChangeSelfInfo(String old_password,String new_password,String new_password2) throws IOException {
if(user_password.equals(old_password)) {
if(new_password.equals(new_password2)) {
sendData("CLIENT_CHANGESELF");
output.writeObject(user_name);
output.flush();
output.writeObject(new_password);
output.flush();
output.writeObject(user_role);
output.flush();
}
else {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(null, "密码错误", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
}
synchronized public static void DeleteUser(String name) throws IOException {
sendData("CLIENT_DELETEUSER");
output.writeObject(name);
output.flush();
}
synchronized public static void UpdateUser(String name,String password,String role) throws IOException {
sendData("CLIENT_UPDATEUSER");
output.writeObject(name);
output.flush();
output.writeObject(password);
output.flush();
output.writeObject(role);
output.flush();
}
synchronized public static void InsertUser(String name, String password,String role) throws IOException {
sendData("CLIENT_INSERTUSER");
output.writeObject(name);
output.flush();
output.writeObject(password);
output.flush();
output.writeObject(role);
output.flush();
}
synchronized public static void DownloadFile(String ID,String filepath) throws IOException {
sendData("CLIENT_DOWNLOADFILE");
output.writeObject(ID);
output.flush();
filepath1=filepath;
}
synchronized public static void UploadFile(String ID,String Creator,String description,String filepath) throws IOException{
sendData("CLIENT_UPLOADFILE");
File file=new File(filepath.trim());
String filename=file.getName();
long fileLength=file.length();
output.writeObject(ID);
output.flush();
output.writeObject(Creator);
output.flush();
output.writeObject(description);
output.flush();
output.writeObject(filename);
output.flush();
output.writeLong(fileLength);
output.flush();
FileInputStream in=new FileInputStream(file);
byte[] buffer=new byte[1024];
int length=in.read(buffer,0,buffer.length);
while(length>0) {
output.write(buffer,0,length);
output.flush();
}
in.close();
}
synchronized public static void Listuser() throws IOException {
sendData("CLIENT_LISTUSER");
}
synchronized public static void ListDoc() throws IOException {
sendData("CLIENT_LISTDOC");
}
synchronized public static void clientclose() throws IOException{
sendData("TERMINATE");
} synchronized public static String get_Name() {
return user_name;
}
synchronized public static String get_Role() {
return user_role;
}
synchronized public static String[][] get_Docs(){
return DocData;
}
synchronized public static String[][] get_Users(){
return UserData;
}
}
Filemanagement.java
package MSystem.GUI;
import java.awt.EventQueue;
import java.awt.FileDialog;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import Internet.Client;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
public class Filemanagement {
public static JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTable table;
public static int d;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Filemanagement window = new Filemanagement();
window.Fileframe(d);
Filemanagement.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
* @return
*/
public void Fileframe(int d) {
initialize(d);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(int a) {
frame = new JFrame();
frame.setTitle("\u6587\u4EF6\u7BA1\u7406\u754C\u9762");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setToolTipText("");
tabbedPane.setBounds(10, 10, 416, 243);
frame.getContentPane().add(tabbedPane);
String[] columnName={
"档案号","作者","时间","文件名","描述"};
String[][] rowData=new String[50][5];
rowData=Client.get_Docs();
String[] DocData=new String[50];
int i=0;
i=Client.z;
for(int j=0;j<i;j++) {
DocData[j]=rowData[j][0];
}
/*Enumeration<Doc> e=null;
try {
e = DataProcessing.getAllDocs();
}
catch (SQLException e1) {
e1.printStackTrace();;
}
Doc doc;
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++;
}*/
JPanel panel = new JPanel();
tabbedPane.addTab("文件上传", null, panel, null);
panel.setLayout(null);
if(Client.get_Role().equals("operator")) {
tabbedPane.setEnabledAt(0, true);
}
else tabbedPane.setEnabledAt(0, false);
JLabel label = new JLabel("\u6863\u6848\u53F7");
label.setBounds(44, 10, 58, 15);
panel.add(label);
JLabel label_1 = new JLabel("\u6863\u6848\u63CF\u8FF0");
label_1.setBounds(44, 51, 58, 15);
panel.add(label_1);
JLabel label_2 = new JLabel("\u6863\u6848\u540D");
label_2.setBounds(44, 126, 58, 15);
panel.add(label_2);
textField = new JTextField();
textField.setBounds(112, 7, 184, 21);
panel.add(textField);
textField.setColumns(10);
JTextArea textArea = new JTextArea();
textArea.setBounds(112, 46, 184, 69);
panel.add(textArea);
textField_1 = new JTextField();
textField_1.setBounds(112, 123, 184, 21);
panel.add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton = new JButton("\u6253\u5F00");
btnNewButton.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
FileDialog fd1=new FileDialog(frame,"打开文件对话框",FileDialog.LOAD);
fd1.show();
textField_1.setText(fd1.getDirectory()+fd1.getFile());
}
});
btnNewButton.setBounds(306, 122, 78, 23);
panel.add(btnNewButton);
JButton button = new JButton("\u4E0A\u4F20");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int op=JOptionPane.showConfirmDialog(null, "确认上传", "温馨提示", JOptionPane.YES_NO_OPTION);
if(op==JOptionPane.YES_OPTION) {
String ID=textField.getText();
String description=textArea.getText();
String filepath=textField_1.getText();
String creator=Client.get_Name();
try {
Client.UploadFile(ID, creator, description, filepath);
} catch (IOException e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
e1.printStackTrace();
}
/*byte[] buffer=new byte[1024];
Timestamp timestamp=new Timestamp(System.currentTimeMillis());
File tempFile=new File(filepath.trim());
String filename=tempFile.getName();
try {
if(DataProcessing.insertDoc(ID,name,timestamp,description,filename)) ;
else JOptionPane.showMessageDialog(null, "存入数据库失败", "温馨提示", JOptionPane.ERROR_MESSAGE);
}
catch (SQLException e1) {
e1.printStackTrace();
}
BufferedInputStream infile = null;
try {
infile = new BufferedInputStream(new FileInputStream(filepath));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedOutputStream targetfile = null;
try {
targetfile = new BufferedOutputStream(new FileOutputStream(new File(uploadpath+filename)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
while(true) {
int byteRead = 0;
try {
byteRead = infile.read(buffer);
} catch (IOException e1) {
e1.printStackTrace();
}
if(byteRead==-1)
break;
try {
targetfile.write(buffer,0,byteRead);
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
infile.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
targetfile.close();
} catch (IOException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "上传成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
frame.dispose();*/
}
}
});
button.setBounds(72, 170, 97, 23);
panel.add(button);
JButton button_1 = new JButton("\u53D6\u6D88");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
button_1.setBounds(199, 170, 97, 23);
panel.add(button_1);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("文件下载", null, panel_1, null);
panel_1.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 391, 120);
panel_1.add(scrollPane);
table = new JTable(rowData,columnName);
scrollPane.setViewportView(table);
JButton button_2 = new JButton("\u4E0B\u8F7D");
button_2.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
int op=JOptionPane.showConfirmDialog(null, "确认下载", "温馨提示", JOptionPane.YES_NO_OPTION);
if(op==JOptionPane.YES_OPTION) {
if(table.getSelectedRow()<0);
else {
FileDialog fd2=new FileDialog(frame,"下载文件",FileDialog.SAVE);
fd2.show();
String filepath=fd2.getDirectory();
String id=(String)table.getValueAt(table.getSelectedRow(), 0);
try {
Client.DownloadFile(id,filepath);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*byte[] buffer=new byte[1024];
Doc doc=null;
try {
doc = DataProcessing.searchDoc(id);
}
catch (SQLException e1) {
e1.printStackTrace();
}
File tempFile=new File(uploadpath+doc.getFilename());
String filename=doc.getFilename();
BufferedInputStream infile = null;
try {
infile = new BufferedInputStream(new FileInputStream(tempFile));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
BufferedOutputStream targetfile = null;
try {
targetfile = new BufferedOutputStream(new FileOutputStream(new File(filepath+filename)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
while(true) {
int byteRead = 0;
try {
byteRead = infile.read(buffer);
} catch (IOException e1) {
e1.printStackTrace();
}
if(byteRead==-1)
break;
try {
targetfile.write(buffer,0,byteRead);
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
infile.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
targetfile.close();
} catch (IOException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "下载成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
*/
}
}
}
});
button_2.setBounds(84, 158, 97, 23);
panel_1.add(button_2);
JButton button_3 = new JButton("\u53D6\u6D88");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
button_3.setBounds(201, 158, 97, 23);
panel_1.add(button_3);
switch(a) {
case 0:
tabbedPane.setSelectedIndex(0);break;
case 1:
tabbedPane.setSelectedIndex(1);break;
default:break;
}
}
}
MainGUI.java
package MSystem.GUI;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import Internet.Client;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class MainGUI {
public static JFrame frame;
JTextField textField;
private JPasswordField passwordField;
public static String role;
public static String name;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@SuppressWarnings("static-access")
public void run() {
MainGUI window = new MainGUI();
window.frame.setVisible(true);
}
});
}
/**
* Create the application.
*/
public MainGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("\u7CFB\u7EDF\u767B\u5F55");
frame.setBounds(100, 100, 658, 388);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801");
lblNewLabel_1.setBounds(103, 150, 76, 38);
frame.getContentPane().add(lblNewLabel_1);
JLabel label = new JLabel("\u7528\u6237\u540D");
label.setBounds(103, 112, 58, 15);
frame.getContentPane().add(label);
textField = new JTextField();
textField.setBounds(178, 109, 222, 21);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton = new JButton("\u786E\u5B9A");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name=textField.getText();
String password=String.valueOf(passwordField.getPassword());
try {
/*User user=DataProcessing.search(name, password1);
role=DataProcessing.getrole(name);
if(user==null)*/
Client.Login(name, password);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(126, 220, 103, 32);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u53D6\u6D88");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
try {
Client.clientclose();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnNewButton_1.setBounds(255, 220, 103, 32);
frame.getContentPane().add(btnNewButton_1);
passwordField = new JPasswordField();
passwordField.setBounds(178, 159, 222, 21);
frame.getContentPane().add(passwordField);
}
}
MenuGUI.java
package MSystem.GUI;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import Internet.Client;
import javax.swing.JMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
public class MenuGUI {
public static JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
@SuppressWarnings("unused")
MenuGUI window=new MenuGUI();
MenuGUI.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MenuGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
Client.clientclose();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 714, 393);
frame.dispose();
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menu = new JMenu("\u7528\u6237\u7BA1\u7406");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("\u4FEE\u6539\u7528\u6237");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.Listuser();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Usermanagement.select=0;
Usermanagement.main(null);
}
});
menu.add(menuItem);
JMenuItem menuItem_2 = new JMenuItem("\u5220\u9664\u7528\u6237");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.Listuser();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Usermanagement.select=1;
Usermanagement.main(null);
}
});
menu.add(menuItem_2);
JMenuItem menuItem_1 = new JMenuItem("\u65B0\u589E\u7528\u6237");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.Listuser();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Usermanagement.select=2;
Usermanagement.main(null);
}
});
menu.add(menuItem_1);
JMenu mnNewMenu = new JMenu("\u6863\u6848\u7BA1\u7406");
menuBar.add(mnNewMenu);
JMenuItem menuItem_3 = new JMenuItem("\u6863\u6848\u4E0A\u4F20");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.ListDoc();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Filemanagement.d=0;
Filemanagement.main(null);
}
});
mnNewMenu.add(menuItem_3);
JMenuItem menuItem_4 = new JMenuItem("\u6863\u6848\u4E0B\u8F7D");
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.ListDoc();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Filemanagement.d=1;
Filemanagement.main(null);
}
});
mnNewMenu.add(menuItem_4);
JMenu mnNewMenu_1 = new JMenu("\u4E2A\u4EBA\u4FE1\u606F\u7BA1\u7406");
menuBar.add(mnNewMenu_1);
JMenuItem menuItem_5 = new JMenuItem("\u4FE1\u606F\u4FEE\u6539");
menuItem_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UserGUI.main(null);
}
});
mnNewMenu_1.add(menuItem_5);
String role=Client.get_Role();
switch(role) {
case "administrator":
frame.setTitle("系统管理员界面");
mnNewMenu.getItem(0).setEnabled(false);
break;
case "operator":
frame.setTitle("档案录入员界面");
menu.setEnabled(false);
break;
case "browser":
frame.setTitle("档案浏览员界面");
menu.setEnabled(false);
mnNewMenu.getItem(0).setEnabled(false);
break;
default:
break;
}
}
}
UserGUI.java
package MSystem.GUI;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import Internet.Client;
import javax.swing.JMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
public class MenuGUI {
public static JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
@SuppressWarnings("unused")
MenuGUI window=new MenuGUI();
MenuGUI.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MenuGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
try {
Client.clientclose();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 714, 393);
frame.dispose();
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menu = new JMenu("\u7528\u6237\u7BA1\u7406");
menuBar.add(menu);
JMenuItem menuItem = new JMenuItem("\u4FEE\u6539\u7528\u6237");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.Listuser();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Usermanagement.select=0;
Usermanagement.main(null);
}
});
menu.add(menuItem);
JMenuItem menuItem_2 = new JMenuItem("\u5220\u9664\u7528\u6237");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.Listuser();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Usermanagement.select=1;
Usermanagement.main(null);
}
});
menu.add(menuItem_2);
JMenuItem menuItem_1 = new JMenuItem("\u65B0\u589E\u7528\u6237");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.Listuser();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Usermanagement.select=2;
Usermanagement.main(null);
}
});
menu.add(menuItem_1);
JMenu mnNewMenu = new JMenu("\u6863\u6848\u7BA1\u7406");
menuBar.add(mnNewMenu);
JMenuItem menuItem_3 = new JMenuItem("\u6863\u6848\u4E0A\u4F20");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.ListDoc();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Filemanagement.d=0;
Filemanagement.main(null);
}
});
mnNewMenu.add(menuItem_3);
JMenuItem menuItem_4 = new JMenuItem("\u6863\u6848\u4E0B\u8F7D");
menuItem_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Client.ListDoc();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Filemanagement.d=1;
Filemanagement.main(null);
}
});
mnNewMenu.add(menuItem_4);
JMenu mnNewMenu_1 = new JMenu("\u4E2A\u4EBA\u4FE1\u606F\u7BA1\u7406");
menuBar.add(mnNewMenu_1);
JMenuItem menuItem_5 = new JMenuItem("\u4FE1\u606F\u4FEE\u6539");
menuItem_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UserGUI.main(null);
}
});
mnNewMenu_1.add(menuItem_5);
String role=Client.get_Role();
switch(role) {
case "administrator":
frame.setTitle("系统管理员界面");
mnNewMenu.getItem(0).setEnabled(false);
break;
case "operator":
frame.setTitle("档案录入员界面");
menu.setEnabled(false);
break;
case "browser":
frame.setTitle("档案浏览员界面");
menu.setEnabled(false);
mnNewMenu.getItem(0).setEnabled(false);
break;
default:
break;
}
}
}
Usermanagement.java
package MSystem.GUI;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import Internet.Client;
import javax.swing.JTable;
import javax.swing.JScrollPane;
public class Usermanagement {
public JFrame frame;
public static int select;
private JPasswordField passwordField;
private JTable table;
private JTextField textField;
private JPasswordField passwordField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Usermanagement window = new Usermanagement();
window.Userframe(select);
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public void Userframe(int select) {
initialize(select);
}
/**
* Initialize the contents of the frame.
*/
private void initialize(int j) {
frame = new JFrame();
frame.setTitle("\u7528\u6237\u7BA1\u7406\u754C\u9762");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setBounds(10, 10, 416, 243);
frame.getContentPane().add(tabbedPane);
String[] columnName={
"用户名","密码","角色"};
String[][] rowData=new String[50][3];
/*Enumeration<User> e=null;
try {
e = DataProcessing.getAllUser();
}
catch (SQLException e1) {
System.out.println(e1.getMessage());
}
User user;*/
int i=Client.i;
rowData=Client.get_Users();
String[] UserData=new String[50];
/*while(e.hasMoreElements()) {
user=e.nextElement();
nameData[i]=rowData[i][0]=user.getName();
rowData[i][1]=user.getPassword();
rowData[i][2]=user.getRole();
i++;
}*/
for(int c=0;c<i;c++) {
UserData[c]=rowData[c][0];
}
JPanel panel = new JPanel();
tabbedPane.addTab("修改用户", null, panel, null);
panel.setLayout(null);
JLabel label = new JLabel("\u7528\u6237\u540D");
label.setBounds(48, 10, 58, 15);
panel.add(label);
JLabel label_1 = new JLabel("\u53E3\u4EE4");
label_1.setBounds(48, 61, 58, 15);
panel.add(label_1);
JLabel label_2 = new JLabel("\u89D2\u8272");
label_2.setBounds(48, 110, 58, 15);
panel.add(label_2);
passwordField = new JPasswordField();
passwordField.setBounds(116, 58, 181, 21);
panel.add(passwordField);
String role[]= {
"administrator","operator","browser"};
JComboBox<?> comboBox = new JComboBox<Object>(role);
comboBox.setBounds(116, 106, 181, 23);
panel.add(comboBox);
JComboBox<?> comboBox_1 = new JComboBox<Object>(UserData);
comboBox_1.setBounds(116, 10, 181, 23);
panel.add(comboBox_1);
JButton button = new JButton("\u4FEE\u6539");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name=(String)comboBox_1.getSelectedItem();
String password=String.valueOf(passwordField.getPassword());
try {
String role=(String)comboBox.getSelectedItem();
Client.UpdateUser(name, password, role);
//JOptionPane.showMessageDialog(null, "修改成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frame.dispose();
}
});
button.setBounds(70, 152, 97, 23);
panel.add(button);
JButton button_1 = new JButton("\u53D6\u6D88");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
button_1.setBounds(177, 152, 97, 23);
panel.add(button_1);
JPanel panel_1 = new JPanel();
tabbedPane.addTab("删除用户", null, panel_1, null);
panel_1.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 391, 118);
panel_1.add(scrollPane);
table = new JTable(rowData,columnName);
scrollPane.setViewportView(table);
JButton button_2 = new JButton("\u5220\u9664");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(table.getSelectedRow()<0) ;
else {
String name=(String) table.getValueAt(table.getSelectedRow(), 0);
try {
Client.DeleteUser(name);
/*if(DataProcessing.deleteUser(name_del))
JOptionPane.showMessageDialog(null, "删除成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
else
JOptionPane.showMessageDialog(null, "账号不存在", "温馨提示", JOptionPane.ERROR_MESSAGE);*/
} catch (HeadlessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frame.dispose();
}
}
});
button_2.setBounds(75, 157, 97, 23);
panel_1.add(button_2);
JButton button_3 = new JButton("\u53D6\u6D88");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
button_3.setBounds(203, 157, 97, 23);
panel_1.add(button_3);
JPanel panel_2 = new JPanel();
tabbedPane.addTab("新增用户", null, panel_2, null);
panel_2.setLayout(null);
JLabel label_3 = new JLabel("\u7528\u6237\u540D");
label_3.setBounds(58, 10, 58, 15);
panel_2.add(label_3);
JLabel label_4 = new JLabel("\u53E3\u4EE4");
label_4.setBounds(58, 53, 58, 15);
panel_2.add(label_4);
JLabel label_5 = new JLabel("\u89D2\u8272");
label_5.setBounds(58, 105, 58, 15);
panel_2.add(label_5);
textField = new JTextField();
textField.setBounds(126, 7, 178, 21);
panel_2.add(textField);
textField.setColumns(10);
passwordField_1 = new JPasswordField();
passwordField_1.setBounds(126, 50, 178, 21);
panel_2.add(passwordField_1);
JComboBox<?> comboBox_2 = new JComboBox<Object>(role);
comboBox_2.setBounds(126, 101, 178, 23);
panel_2.add(comboBox_2);
JButton button_4 = new JButton("\u589E\u52A0");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name=textField.getText();
String password=String.valueOf(passwordField_1.getPassword());
String role=(String)comboBox_2.getSelectedItem();
try {
Client.InsertUser(name, password, role);
/*DataProcessing.insertUser(name, password, role);
JOptionPane.showMessageDialog(null, "添加成功", "温馨提示", JOptionPane.PLAIN_MESSAGE);
frame.dispose();*/
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
frame.dispose();
}
});
button_4.setBounds(69, 155, 97, 23);
panel_2.add(button_4);
JButton button_5 = new JButton("\u53D6\u6D88");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
button_5.setBounds(190, 155, 97, 23);
panel_2.add(button_5);
switch(j) {
case 0:
tabbedPane.setSelectedIndex(0);
break;
case 1:
tabbedPane.setSelectedIndex(1);
break;
case 2:
tabbedPane.setSelectedIndex(2);
break;
default:break;
}
}
}
总结
到这里,整个实验项目就全部结束了。如果有什么疑惑的话,可以到我的博客去参考前面几篇文章哦。
我的博客