2.利用套接字、多线程、GUI和数据库,参考[例13-简单的C-S结构程序]或[例13-基于C-S结构的学生基本信息管理系统],实现一个C/S结构的查询服务器上数据库表的程序,可以按学号查询、按姓名查询。[50]写出Java示例代码
时间: 2024-03-15 17:45:44 浏览: 103
以下是一个简单的Java示例代码,实现了一个C/S结构的查询服务器上数据库表的程序,可以按学号查询、按姓名查询。代码使用了套接字、多线程、GUI和数据库技术。
服务器端代码:
```
import java.io.*;
import java.net.*;
import java.sql.*;
public class Server {
private static final int PORT = 54321;
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("Server started...");
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket);
Thread clientThread = new Thread(new ClientHandler(clientSocket));
clientThread.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static class ClientHandler implements Runnable {
private Socket clientSocket;
public ClientHandler(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String request = in.readLine();
System.out.println("Received request: " + request);
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");
Statement statement = connection.createStatement();
if (request.equals("QUERY_BY_ID")) {
String id = in.readLine();
ResultSet resultSet = statement.executeQuery("SELECT * FROM students WHERE id = " + id);
while (resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
out.println(name + "\t" + age);
}
} else if (request.equals("QUERY_BY_NAME")) {
String name = in.readLine();
ResultSet resultSet = statement.executeQuery("SELECT * FROM students WHERE name = '" + name + "'");
while (resultSet.next()) {
String id = resultSet.getString("id");
int age = resultSet.getInt("age");
out.println(id + "\t" + age);
}
}
connection.close();
clientSocket.close();
System.out.println("Client disconnected: " + clientSocket);
} catch (IOException | SQLException e) {
e.printStackTrace();
}
}
}
}
```
客户端代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Client {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 54321;
private JFrame frame;
private JTextField idTextField;
private JTextField nameTextField;
private JTextArea resultTextArea;
public static void main(String[] args) {
new Client().start();
}
public void start() {
frame = new JFrame("Student Query");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
JLabel idLabel = new JLabel("ID");
idTextField = new JTextField();
JLabel nameLabel = new JLabel("Name");
nameTextField = new JTextField();
panel.add(idLabel);
panel.add(idTextField);
panel.add(nameLabel);
panel.add(nameTextField);
JButton idButton = new JButton("Query by ID");
idButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
queryById();
}
});
JButton nameButton = new JButton("Query by Name");
nameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
queryByName();
}
});
panel.add(idButton);
panel.add(nameButton);
frame.add(panel, BorderLayout.NORTH);
resultTextArea = new JTextArea();
frame.add(new JScrollPane(resultTextArea), BorderLayout.CENTER);
frame.setVisible(true);
}
private void queryById() {
try {
Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("QUERY_BY_ID");
out.println(idTextField.getText());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
resultTextArea.setText("");
String line;
while ((line = in.readLine()) != null) {
resultTextArea.append(line + "\n");
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void queryByName() {
try {
Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("QUERY_BY_NAME");
out.println(nameTextField.getText());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
resultTextArea.setText("");
String line;
while ((line = in.readLine()) != null) {
resultTextArea.append(line + "\n");
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
注意:上述代码只是一个示例,需要根据实际情况进行修改和完善。例如,需要根据实际数据库的情况修改连接字符串、用户名和密码;需要添加异常处理和错误提示等等。
阅读全文