模拟登入页面

今天我们学习了模拟一个登入页面。
首先:打开SQL,创建一个表tb_user,具体参数如下:
在这里插入图片描述

接着:创建几个包,包里创建几个文件,截图如下
在这里插入图片描述
Music、DBUtil和Test1是上节课的,我们忽略就好。

MusicDao中输入代码:
package com.zhongruan.Dao;

import com.didi.model.Music;
import com.zhongruan.Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MusicDao {

public static List<Music> findMusics(){
    ResultSet resultSet = null;
    PreparedStatement statement = null;
    Connection connection = null;
    List<Music> musics=new ArrayList<>();
    try {
        connection = DBUtil.getConnection();
        String sql = "select * from music";
        statement = connection.prepareStatement(sql);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            Music music = new Music();
            music.setId(resultSet.getInt(1));
            music.setName(resultSet.getString(2));
            music.setAuthor(resultSet.getString(3));
            musics.add(music);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DBUtil.closeAll(resultSet, statement, connection);
    }
    return musics;

}
public static void delete(int id) throws SQLException{
    Connection connection= DBUtil.getConnection();
    String sql="delete from music where id=?";
    PreparedStatement statement=connection.prepareStatement(sql);
    statement.setInt(1,id);
    statement.executeUpdate();
    DBUtil.closeAll(null,statement,connection);
}
public static void charu(String name, String author) throws SQLException {
    Connection connection=DBUtil.getConnection();
    String sql="insert into music(name,author) values(?,?)";
    PreparedStatement statement=connection.prepareStatement(sql);
    statement.setString(1,name);
    statement.setString(2,author);
    statement.executeUpdate();
    DBUtil.closeAll(null,statement,connection);
}



public static void  updateMusic(int id,String newname) throws SQLException{
    Connection connection=DBUtil.getConnection();
    String sql="update music set name=? where id=?";
    PreparedStatement statement=connection.prepareStatement(sql);
    statement.setString(1,newname);
    statement.setInt(2,id);
    statement.executeUpdate();
    DBUtil.closeAll(null,statement,connection);
}

}

在UserDao中输入代码:
package com.zhongruan.Dao;

import com.zhongruan.Util.DBUtil;
import com.zhongruan.model.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class UserDao {
public static User findUserByUsername(String username){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
User user=null;
try{
connection= DBUtil.getConnection();
String sql=“select * from tb_user where username=?”;
statement=connection.prepareStatement(sql);
statement.setString(1,username);
resultSet=statement.executeQuery();
while (resultSet.next()){
user=new User();
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return user;
}
public void churu(String username,String passwod){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try {
connection=DBUtil.getConnection();
String sql=“insert into tb_user(username,password) values(?,?)”;
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,passwod);
statement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
}
}

在LoginException输入代码:
package com.zhongruan.exception;

public class LoginException extends Exception{
public LoginException(String message){
super(message);
}
}

在User中输入代码:
package com.zhongruan.model;

public class User {
private String username;
private String password;
private int type;
public int getType() {
return type;
}

public void setType(Integer type) {
    this.type = type;
}



public String getUsername() {
    return username;
}

@Override
public String toString() {
    return "User{" +
            "username='" + username + '\'' +
            ", password='" + password + '\'' +
            ", type=" + type +
            '}';
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

在View中输入代码:
package com.zhongruan.Util;

import com.didi.model.Music;
import com.zhongruan.Dao.MusicDao;
import com.zhongruan.Dao.UserDao;
import com.zhongruan.exception.LoginException;
import com.zhongruan.model.User;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class View {
public static void main(String[] args) throws LoginException, SQLException {
boolean flag = true;
while (flag) {
Scanner input = new Scanner(System.in);
System.out.println(“请输入用户名”);
String username = input.next();
System.out.println(“请输入密码”);
String passwod = input.next();

        UserDao userDao = new UserDao();
        User user = UserDao.findUserByUsername(username);
        if (user == null) {
            System.out.println("---该用户尚未注册。请先注册---");
            System.out.println("请输入注册名");
            String name = input.next();
            System.out.println("请输入注册码");
            String pw = input.next();
            userDao.churu(name, pw);

        } else if (user.getPassword().equals(passwod)) {
            System.out.println("---欢迎来到音乐管理系统---");
            musicSystem();
            flag=false;
        } else {
            //System.out.println("---密码输入错误,请重新输入");
            throw new LoginException("登入失败");

        }
    }
}
public static void musicSystem() throws SQLException {
    Scanner input=new Scanner(System.in);
    System.out.println("1.音乐查询 2.音乐添加 3.音乐修改 4.音乐删除 5退出");
    int choice=input.nextInt();
    switch (choice){
        case 1:
            //1.音乐查询
            List<Music> musics=MusicDao.findMusics();
            System.out.println(musics);
            break;
        case 2:
            System.out.println("请输入要输入的音乐名");
            String ym=input.next();
            System.out.println("请输入插入的作者名");
            String au=input.next();
            MusicDao.charu(ym,au);
            break;
        case 3:
            System.out.println("请输入要删除的id");
            int id=input.nextInt();
            MusicDao.delete(id);
        case 4:
            System.out.println("请输入要修改的 id");
            int id1=input.nextInt();
            System.out.println("请输入新的音乐名");
            String newym=input.next();
            System.out.println("请输入新的作者名");
            String newau=input.next();
            MusicDao.updateMusic(id1,newym,newau);
            break;
        case 5:
            System.exit(0);

    }

}

}

代码到这个步骤就已经写完了,接下来在View中运行,看一下结果怎么样。
在这里插入图片描述
打开SQL,看看有没有添加上用户
在这里插入图片描述
添加上了就成功了。
接着就运行来一步步测试,看看结果:
在这里插入图片描述
打开sql看结果,首先是用户,其次是music:
在这里插入图片描述
在这里插入图片描述
修改:
在这里插入图片描述
SQL:
在这里插入图片描述
查询:
在这里插入图片描述
删除:
在这里插入图片描述
SQL:
在这里插入图片描述
退出:
在这里插入图片描述

密码错误:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值