文章目录
前言
完成一个简单的用户管理系统,其中用户的信息包含以下:用户id/用户名/密码/性别/生日/手机号/账户状态
要求实现以下功能:
- 用户登录
- 用户添加
- 根据id查询用户
- 查询所有用户
- 修改指定id的用户密码
- 修改指定id用户的登录状态
- 根据id删除指定用户
一、先创建一个People数据库表
用户状态是0的时候表示账号不可用,1的时候表示账号可用
二、用户登录
连接数据库,通过查询找到对应的账号和密码,然后再判断账号密码的对错,还要再判断账号的状态是否可用。
代码如下(示例):
public boolean login(String name, String password) throws ClassNotFoundException, SQLException {
String sql = "select * from people where name=? and password=? ";
Class.forName("com.mysql.jdbc.Driver");
ResultSet rs = null;
try (
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "123456");
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
int i = rs.getInt(7);
if (i == 1) {
return true;
} else {
System.out.println("账号不可用");
}
} else {
System.out.println("账号密码错误");
}
} finally {
rs.close();
}
return false;
}
三、用户添加
连接数据库,添加输入的账号密码和账号状态,因为时间关系就没有加其他的,只加了三个数据,你们可以把每一个数据都加进去。
代码如下(示例):
public boolean add(String name1, String password1, int statu1) throws ClassNotFoundException, SQLException {
String sql = "insert into people(name,password,statu) values(?,?,?)";
try(
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "123456");
PreparedStatement ps = conn.prepareStatement(sql);
) {
ps.setString(1, name1);
ps.setString(2, password1);
ps.setInt(3, statu1);
int i = ps.executeUpdate();
return i >0;
}
}
。
四、根据id查询用户
连接数据库,通过输入的账号id来查询账号的信息,最后还要关闭连接。
public void queryById(int id) throws SQLException {
String sql = "select * from people where id = ? ";
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();
if(rs.next()){
int uid = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
String sex = rs.getString(4);
String birthday = rs.getString(5);
String phone = rs.getString(6);
int statu = rs.getInt(7);
System.out.println(uid+"/"+name+"/"+password+"/"+sex+"/"+birthday+"/"+phone+"/"+statu);
}
rs.close();
ps.close();
conn.close();
}
五、 查询所有用户
连接数据库,直接查询所有账号的所有信息,最后也要关闭连接。
public void ShowAll() throws SQLException {
String sql = "select * from people ";
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
int uid = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
String sex = rs.getString(4);
String birthday = rs.getString(5);
String phone = rs.getString(6);
int statu = rs.getInt(7);
System.out.println(uid+"/"+name+"/"+password+"/"+sex+"/"+birthday+"/"+phone+"/"+statu);
}
rs.close();
ps.close();
conn.close();
}
六、修改指定id的用户密码
连接数据库,根据输入的账号id,和所要更改的密码,来查询并更改账户密码。
public void Changepassword(int id,String newPwd) throws SQLException {
String sql = "update people set password = ? where id= ?";
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement ps = conn.prepareStatement(sql);
){
ps.setString(1,newPwd);
ps.setInt(2,id);
int i = ps.executeUpdate();
if(i>0){
ShowAll();
}
}
}
七、 修改指定id用户的登录状态
连接数据库,通过输入的账号id和所需要更改的账号状态,来查询并更改账号状态。
public void Changstatu(int id ,int Newstatu) throws SQLException {
String sql = "update people set statu = ? where id = ?";
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement ps = conn.prepareStatement(sql);
){
ps.setInt(1,Newstatu);
ps.setInt(2,id);
int i = ps.executeUpdate();
if(i>0){
ShowAll();
}
}
}
八、根据id删除指定用户
连接数据库,通过输入的账号id,来删除账户的所有信息。
public void Delet(int id ) throws SQLException {
String sql = "delete from people where id = ?";
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
PreparedStatement ps = conn.prepareStatement(sql);
) {
//预处理(向sql语句指定位置的占位符填充实际值)
ps.setInt(1, id);
int i = ps.executeUpdate();
if(i>0){
ShowAll();
}
}
}
九、用户的操作界面
用户可用通过输入对应的数字来对数据库进行操作。
public class Test {
Scanner scan = new Scanner(System.in);
PeopleManage pm = new PeopleManage();
public void mainMenu() throws SQLException, ClassNotFoundException {
System.out.println(("**********欢迎使用SOFTEEM-SMART通信录*******"));
System.out.println("*\t[1]用户登录\t\t\t*");
System.out.println("*\t[2]用户添加\t\t\t*");
System.out.println("*\t[3]根据id查询用户\t\t*");
System.out.println("*\t[4]查询所有用户\t\t*");
System.out.println("*\t[5]修改指定id的用户密码\t\t\t*");
System.out.println("*\t[6]修改指定id用户的登录状态\t\t\t*");
System.out.println("*\t[7]根据id删除指定用户\t\t\t*");
System.out.println("*\t[0]退出\t\t\t\t*");
System.out.println("*****************************************");
System.out.println("请输入正确的操作指令:");
//开始
start();
}
public void start() throws SQLException, ClassNotFoundException {
String s = scan.next();
if(!s.matches("^[0-7]$")) {
System.out.println("请输入合法的操作指令");
start();
}
switch(s) {
case "1":
//用户登录
login();
break;
case "2":
//用户添加
add();
break;
case "3":
//根据id查询用户
queryById();
break;
case "4":
//查询所有用户
ShowAll();
break;
case "5":
//修改指定id的用户密码
Changepassword();
break;
case "6":
//修改指定id用户的登录状态
Changstatu();
break;
case "7":
//根据id删除指定用户
Delet();
break;
case "0":
//退出
System.out.println("谢谢使用,再见!");
System.exit(0);
break;
}
}
public void login() throws SQLException, ClassNotFoundException {
System.out.println("请输入正确的账号");
String a = scan.next();
System.out.println("请输入正确的密码");
String b = scan.next();
boolean f = pm.login(a,b);
System.out.println(f ? "登录成功" : "登录失败");
mainMenu();
}
public void add() throws SQLException, ClassNotFoundException {
System.out.println("请输入需要添加的用户名");
String a = scan.next();
System.out.println("请输入需要添加的密码");
String b = scan.next();
System.out.println("请输入需要添加的账号状态");
int c = scan.nextInt();
pm.add(a,b,c);
pm.ShowAll();
mainMenu();
}
public void queryById() throws SQLException, ClassNotFoundException {
System.out.println("请输入需要查询的id");
int i = scan.nextInt();
pm.queryById(i);
mainMenu();
}
public void ShowAll() throws SQLException, ClassNotFoundException {
pm.ShowAll();
mainMenu();
}
public void Changepassword() throws SQLException, ClassNotFoundException {
System.out.println("请输入需要修改的id");
int a = scan.nextInt();
System.out.println("请输入需要修改的密码");
String b = scan.next();
pm.Changepassword(a,b);
pm.ShowAll();
mainMenu();
}
public void Changstatu() throws SQLException, ClassNotFoundException {
System.out.println("请输入需要修改的id");
int a = scan.nextInt();
System.out.println("请输入需要修改的账号状态");
int b = scan.nextInt();
pm.Changstatu(a,b);
pm.ShowAll();
mainMenu();
}
public void Delet() throws SQLException, ClassNotFoundException {
System.out.println("请输入删除修改的id");
int a = scan.nextInt();
pm.Delet(a);
pm.ShowAll();
mainMenu();
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
new Test().mainMenu();
}
}
十、总结
我们还没学到框架,所以有很多重复的代码,见谅啊