用户登录和注册系统的代码,将用户名、密码和上次登录时间存在MySQL数据库中,构建Users类,用于存放用户对象;创建Utils类,用于提供数据库连接和关闭方法;创建UsersDao类,用于提供数据库接口。
时间: 2024-02-22 15:00:37 浏览: 77
好的,以下是一个简单的用户登录和注册系统的代码示例,包括将用户名、密码和上次登录时间存在MySQL数据库中,构建Users类,用于存放用户对象;创建Utils类,用于提供数据库连接和关闭方法;创建UsersDao类,用于提供数据库接口。请注意,这只是一个简单的示例,您需要根据自己的实际需求进行调整和完善。
1. Users类
```java
public class Users {
private int id;
private String username;
private String password;
private String lastLoginTime;
// 构造方法
public Users(int id, String username, String password, String lastLoginTime) {
this.id = id;
this.username = username;
this.password = password;
this.lastLoginTime = lastLoginTime;
}
// getter 和 setter 方法
// ...
}
```
2. Utils类
```java
public class Utils {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/test";
private static final String username = "root";
private static final String password = "123456";
// 获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 关闭数据库连接
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 关闭Statement
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 关闭ResultSet
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
3. UsersDao类
```java
public class UsersDao {
// 添加用户
public static boolean addUser(Users user) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = Utils.getConnection();
String sql = "INSERT INTO users (username, password, last_login_time) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getLastLoginTime());
int count = pstmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Utils.closeStatement(pstmt);
Utils.closeConnection(conn);
}
return false;
}
// 根据用户名查询用户
public static Users getUserByUsername(String username) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = Utils.getConnection();
String sql = "SELECT * FROM users WHERE username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String password = rs.getString("password");
String lastLoginTime = rs.getString("last_login_time");
Users user = new Users(id, username, password, lastLoginTime);
return user;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Utils.closeResultSet(rs);
Utils.closeStatement(pstmt);
Utils.closeConnection(conn);
}
return null;
}
// 更新用户的上次登录时间
public static boolean updateLastLoginTime(Users user) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = Utils.getConnection();
String sql = "UPDATE users SET last_login_time=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getLastLoginTime());
pstmt.setInt(2, user.getId());
int count = pstmt.executeUpdate();
if (count > 0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Utils.closeStatement(pstmt);
Utils.closeConnection(conn);
}
return false;
}
}
```
以上代码示例仅供参考,具体实现可以根据您的需求进行调整。希望能对您有所帮助!
阅读全文