07.26 密码重置 java mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class ResetPsw {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--------------重置密码---------------");
System.out.println("请输入用户名:");
String username = scanner.next();
System.out.println("请输入新密码:");
String npsw = scanner.next();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "199702110";
PreparedStatement pstmt =null;
String sql = " update user set password = ? where username = ? ";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, npsw);
pstmt.setString(2, username);
pstmt.executeUpdate(); //注意
System.out.println("重置密码成功!");
} catch (Exception e) {
System.out.println("重置密码失败!");
e.printStackTrace();
}finally {
try {
if(pstmt != null) {
pstmt.close();
}
if(conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}