登陆账号后,用户更改自己的登陆密码和个人信息的java和mysql代码
时间: 2024-03-15 17:42:25 浏览: 48
用户登陆用户密码修改代码
4星 · 用户满意度95%
以下是Java代码示例:
更改密码:
```java
public void updatePassword(String userId, String newPassword) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
String sql = "UPDATE user SET password = ? WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, newPassword);
ps.setString(2, userId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, ps, null);
}
}
```
更新个人信息:
```java
public void updateUserInfo(String userId, String newName, int newAge) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
String sql = "UPDATE user SET name = ?, age = ? WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, newName);
ps.setInt(2, newAge);
ps.setString(3, userId);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(conn, ps, null);
}
}
```
以下是MySQL代码示例:
更改密码:
```sql
UPDATE user SET password = 'newPassword' WHERE id = 'userId';
```
更新个人信息:
```sql
UPDATE user SET name = 'newName', age = 'newAge' WHERE id = 'userId';
```
阅读全文