day10 音乐管理编写小记

音乐管理系统之用户管理

添加用户模块

  1. 加载驱动
  2. 创建连接
  3. 编写sql语句
  4. 得到statment对象
  5. 查询,返回结果
  • sql语句中的值如何替换?
String sql="insert into tb_user (username,password,type) values (?,?,?)";

​ 通过PreparedStatement statement = connection.prepareStatement(sql);得到statment对象后,用setString或setInt方法替换(视数据类型)。

public Boolean zhuce(String username,String password,int type) throws SQLException {
        Connection connection=DBUtil.getConnection();
        String sql="insert into tb_user (username,password,type) values (?,?,?);";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,username);
        statement.setString(2,password);
        statement.setInt(3,type);
        int i = statement.executeUpdate();
        if(i!=0){
            System.out.println("修改成功");
            return true;
        }else{
            System.out.println("修改失败");
            return false;
        }

    }
  • List集合保存数据

    • List 中E可以是实体类.例如:List<User> users= new ArrayList<>();
    1. 创建:List<String> list = new ArrayList<>();
    2. 添加数据list.add("test");
  • ArrayList是什么?

    xxx

其他模块

  • 同添加用户模块类似,修改sql语句即可。

  • 查询模块需使用executeQuery方法返回结果集。

递归

  • 自己调用自己,称为递归。

  • 当程序需要重新运行自己的时候,直接写自己的方法名即可重新调用。

public void getMenu() throws SQLException {
        UserDao userDao=new UserDao();
        MusicDao musicDao = new MusicDao();
        Scanner input = new Scanner(System.in);
        System.out.println("-------------登陆成功-------------");
        System.out.println("-------------欢迎来到音乐管理系统-------------");
        System.out.println("\t1.音乐管理\t2.用户管理\t3.退出系统");
        int j=input.nextInt();
        if (j==1){
            System.out.println("-------------欢迎来到音乐管理功能-------------");
            System.out.println("\t1.查询音乐\t2.添加音乐\t3.修改音乐\t4.删除音乐\t5.返回上一层");
            int a=input.nextInt();
            int id=-1;
            String musicname;
            String author;
            switch(a){
                case 1:
                    List<Music> musics=musicDao.findAll();
                    System.out.println(musics);
                    break;
                case 2:
                    System.out.println("请输入音乐名:");
                    musicname=input.next();
                    System.out.println("请输入作者:");
                    author=input.next();
                    Music music=new Music();
                    music.setMusicname(musicname);
                    music.setAuthor(author);

                    musicDao.add(music);
                    break;
                case 3:
                    System.out.println("请输入需要修改的音乐id:");
                    id=input.nextInt();
                    System.out.println("请输入修改后的音乐名");
                    musicname=input.next();
                    System.out.println("请输入修改后的作者");
                    author=input.next();
                    musicDao.update(id,musicname,author);
                    break;
                case 4:
                    System.out.println("请输入需要删除的音乐id:");
                    id=input.nextInt();
                    musicDao.del(id);
                    break;
                case 5:
                    getMenu();		//==递归==
                    break;
            }
        }else if (j==2){
            System.out.println("-------------欢迎来到用户管理功能-------------");
            System.out.println("\t1.查询用户\t2.添加用户\t3.修改用户\t4.删除用户\t5.返回上一层");
            int b=input.nextInt();
            switch(b){
                case 1:
                    List<User> users = userDao.findAll();
                    System.out.println(users);
                    break;
                case 2:
                    System.out.println("请输入用户名:");
                    String username = input.next();
                    System.out.println("请输入密码:");
                    String password = input.next();
                    System.out.println("请输入角色:");
                    int type=input.nextInt();

                    userDao.zhuce(username,password,type);
                    break;
                case 3:
                    System.out.println("请输入要修改的id:");
                    int id = input.nextInt();
                    System.out.println("请输入修改后的用户名");
                    String newusername = input.next();
                    System.out.println("请输入修改后的密码");
                    String newpassword = input.next();
                    System.out.println("请输入修改后角色");
                    int newtype = input.nextInt();
                    userDao.update(id, newusername, newpassword, newtype);
                    break;
                case 4:
                    System.out.println("请输入需要删除的id");
                    id = input.nextInt();
                    userDao.del(id);
                    break;
            }





        }else if(j==3){
            System.exit(0);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值