Java C/S实现账号密码登录,给出参考代码
时间: 2024-02-01 14:02:24 浏览: 95
好的,以下是Java C/S实现账号密码登录的参考代码。请注意,这仅是一个基本的示例,实际应用中还需要进行异常处理、密码加密等操作,以保证安全性和稳定性。
客户端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
// 创建Socket对象,指定服务器地址和端口号
Socket socket = new Socket("127.0.0.1", 8888);
// 获取输入输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 读取用户输入的账号和密码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入账号:");
String username = reader.readLine();
System.out.print("请输入密码:");
String password = reader.readLine();
// 向服务器发送账号和密码
out.println(username);
out.println(password);
// 读取服务器返回的结果
String result = in.readLine();
System.out.println(result);
// 关闭流和Socket
in.close();
out.close();
socket.close();
}
}
```
服务器端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;
public class Server {
public static void main(String[] args) throws IOException {
// 创建ServerSocket对象,指定监听端口号
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
// 监听客户端连接
Socket socket = serverSocket.accept();
System.out.println("客户端已连接,地址为:" + socket.getInetAddress());
// 获取输入输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
// 读取客户端发送的账号和密码
String username = in.readLine();
String password = in.readLine();
// 验证账号和密码
if (checkUser(username, password)) {
out.println("登录成功!");
} else {
out.println("账号或密码错误,请重新输入!");
}
// 关闭流和Socket
in.close();
out.close();
socket.close();
serverSocket.close();
}
private static boolean checkUser(String username, String password) {
boolean result = false;
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
// 加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
// 创建PreparedStatement对象
statement = connection.prepareStatement("select * from user where username=? and password=?");
statement.setString(1, username);
statement.setString(2, password);
// 执行查询操作
resultSet = statement.executeQuery();
// 判断是否查询到结果
if (resultSet.next()) {
result = true;
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 关闭ResultSet、PreparedStatement和Connection
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}
```
请注意,此处使用的是MySQL数据库,需要先安装MySQL,并创建名为test的数据库,同时创建名为user的表,包含两个字段:username和password。在实际应用中,可以根据需要进行修改。
阅读全文