(8.16)

星期五我们完善了音乐管理系统的功能代码
下星期要开始自己写项目了,但感觉写代码有些知识还不是很熟练,还是要自己课后多查资料。

音乐管理系统代码:
MusicService:
package com.zhongruan.service;

import com.zhongruan.bean.User;
import com.zhongruan.dao.Music;
import com.zhongruan.dao.MusicDao;
import com.zhongruan.dao.UserDao;

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

public class MusicService {
public static void main(String[] args) throws SQLException {
UserDao userDao = new UserDao();
MusicService musicService = new MusicService();
boolean flag = true;
while (flag) {
System.out.println("-----------------------请输入用户名:----------------------");
Scanner input = new Scanner(System.in);
String username = input.next();
System.out.println("------------------------请输入密码:-----------------------");
String password = input.next();
boolean a = userDao.login(username, password);
if(a) {
musicService.getMenu();
}else{
musicService.zhuce();
}
break;
}
}

public void getMenu() throws SQLException {
    MusicDao musicDao = new MusicDao();
    Scanner input = new Scanner(System.in);
    System.out.println("-------------------------登入成功--------------------------");
    System.out.println("--------------------欢迎来到音乐管理系统--------------------");
    System.out.println("\t\t1.音乐管理\t\t2.用户管理\t\t3.退出系统");
    int j = input.nextInt();
    if(j == 1){
        System.out.println("--------------------欢迎进入音乐管理模块--------------------");
        System.out.println(" 1.查询音乐\t2.添加音乐\t3.修改音乐\t4.删除音乐\t5.返回上一层");
        int a = input.nextInt();
        switch (a) {
            case 1:
                List <Music> musics = MusicDao.findAll();
                System.out.println(musics);
                break;
            case 2:
                System.out.println("请输入要添加的音乐名称:");
                String musicname = input.next();
                System.out.println("请输入要添加的作者:");
                String author = input.next();
                Music music = new Music();
                music.setMusicname(musicname);
                music.setAuthor(author);
                musicDao.add(music);
                break;
            case 3:
                System.out.println("------------------------请选择修改项-----------------------");
                System.out.println("\t\t\t1.修改作者\t\t\t\t2.修改音乐名");
                MusicDao.update();
                break;
            case 4:
                System.out.println("请输入要删除歌曲的id");
                int id =  input.nextInt();
                musicDao.delete(id);
                break;
            case 5:
                getMenu();
                break;
        }
    }else if(j == 2){
        System.out.println("--------------------欢迎进入用户管理模块--------------------");
        System.out.println(" 1.查询用户\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();
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);
                UserDao.add(user);
                break;
            case 3:
                System.out.println("------------------------请选择修改项-----------------------");
                System.out.println("\t\t\t1.修改用户名\t\t\t\t2.修改密码");
                UserDao.update();
                break;
            case 4:
                System.out.println("请输入要删除用户的id");
                int id =  input.nextInt();
                UserDao.delete(id);
                break;
            case 5:
                getMenu();
                break;
        }
    }
}

public void zhuce() throws SQLException {
    UserDao userDao = new UserDao();
    Scanner input = new Scanner(System.in);
    System.out.println("-------------------------登入失败--------------------------");
    System.out.println("-------------------------请先注册--------------------------");
    System.out.println("请输入注册名:");
    String zcm = input.next();
    System.out.println("请输入注册密码:");
    String zcmm1 = input.next();
    System.out.println("请确认密码:");
    String zcmm2 = input.next();
    System.out.println("请输入角色:");
    int js = input.nextInt();
    if(zcmm1.equals(zcmm2) && (js == 0 || js == 1)){
        userDao.zhuce(zcm, zcmm1, js);
        System.out.println("注册成功!");
        System.out.println("是否登入(Y)/退出(N)");
        String i = input.next();
        if(i.equals("N")){
            System.out.println("谢谢您注册!");
        }else if(i.equals("Y")){
            getMenu();
        }else{
            System.out.println("输入有误!");
        }
    }else{
        System.out.println("输入有误,请重新注册!");
    }
}

}

userdao:
package com.zhongruan.dao;

import com.zhongruan.bean.User;
import com.zhongruan.until.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;
import java.util.Scanner;

public class UserDao {
public boolean login(String username, String password) throws SQLException {
Connection connection = DBUtil.getConnection();
String sql = “select * from tb_user where username=? and password=?”;
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2,password);
ResultSet resultSet = statement.executeQuery();
if(resultSet.next()){
return true;
}else{
return false;
}
}

public static boolean login2(String username, String password) throws SQLException {
    Connection connection = DBUtil.getConnection();
    PreparedStatement statement = connection.prepareStatement("SELECT * FROM tb_user where username = ? and password = ?");
    statement.setString(1,username);
    statement.setString(2,password);
    ResultSet resultSet = statement.executeQuery();
    int type = 0;
    if(resultSet.next()) {
        type=resultSet.getInt(4);
    }
    if(type == 1){
        return true;
    }else {
        return false;
    }
}

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){
        return true;
    }else{
        return false;
    }
}

public static List <User> findAll() throws SQLException {
    List <User> users = new ArrayList<>();
    Connection connection = DBUtil.getConnection();
    PreparedStatement statement = connection.prepareStatement("SELECT * FROM music");
    ResultSet resultSet = statement.executeQuery();
    while (resultSet.next()){
        int id = resultSet.getInt(1);
        String username = resultSet.getString(2);
        String password = resultSet.getString(3);
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        users.add(user);
    }
    return users;
}

public static void add(User user) throws SQLException{
    Scanner input = new Scanner(System.in);
    Connection connection = DBUtil.getConnection();
    PreparedStatement statement = connection.prepareStatement("insert into tb_user (username, password, type) value (?, ?, ?)");
    System.out.println("请输入用户名:");
    String username = input.next();
    System.out.println("请输入密码:");
    String password = input.next();
    if(login2(username, password)) {
        statement.setString(1,user.getUsername());
        statement.setString(2,user.getPassword());
        statement.setInt(3,user.getType());
        int i = statement.executeUpdate();
        if(i == 1){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }else {
        System.out.println("添加失败");
    }
}

public static void update() throws SQLException {
    Scanner input = new Scanner(System.in);
    Connection connection = DBUtil.getConnection();
    int j = input.nextInt();
    if(j == 1){
        PreparedStatement preparedStatement = connection.prepareStatement("update tb_user set username = ? where username = ?");
        System.out.println("请输入您要修改的用户名:");
        String username1 = input.next();
        System.out.println("请输入您要修改的原用户名:");
        String username2 = input.next();
        preparedStatement.setString(1,username1);
        preparedStatement.setString(2,username2);
        int i = preparedStatement.executeUpdate();
        if(i == 1){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }else {
        PreparedStatement preparedStatement = connection.prepareStatement("update tb_user set password = ? where password = ?");
        System.out.println("请输入您要修改密码:");
        String password1 = input.next();
        System.out.println("请输入您要修改的原密码:");
        String password2 = input.next();
        preparedStatement.setString(1,password1);
        preparedStatement.setString(2,password2);
        int i = preparedStatement.executeUpdate();
        if(i == 1){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }
}

public static void delete(int id) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    connection = DBUtil.getConnection();
    int i = 0;
    try{
        statement = connection.prepareStatement("delete from tb_user where id = ?");
        statement.setInt(1,id);
        i = statement.executeUpdate();
        if(i != 0){
            System.out.println("删除成功");

        }else{
            System.out.println("删除失败");
        }
    }catch (SQLException e){
        e.printStackTrace();
    }
}

}

Musicdao:
package com.zhongruan.dao;

import com.zhongruan.until.DBUtil;
import jdk.internal.util.xml.impl.Input;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MusicDao {
public static List findAll() throws SQLException {
List musics = new ArrayList <>();
Connection connection = DBUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(“SELECT * FROM music”);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt(1);
String musicname = resultSet.getString(2);
String author = resultSet.getString(3);
Music music = new Music();
music.setId(id);
music.setMusicname(musicname);
music.setAuthor(author);
musics.add(music);
}
return musics;
}

public void add(Music music) throws SQLException{
    Scanner input = new Scanner(System.in);
    Connection connection = DBUtil.getConnection();
    PreparedStatement statement = connection.prepareStatement("insert into music (musicname, author) value (?, ?)");
    statement.setString(1,music.getMusicname());
    statement.setString(2,music.getAuthor());
    int i = statement.executeUpdate();
    if(i == 1){
        System.out.println("添加成功");
    }else{
        System.out.println("添加失败");
    }
}

public static void update() throws SQLException {
    Scanner input = new Scanner(System.in);
    Connection connection = DBUtil.getConnection();
    int j = input.nextInt();
    if(j == 1){
        PreparedStatement preparedStatement = connection.prepareStatement("update music set author = ? where author = ?");
        System.out.println("请输入您要修改的作者:");
        String author1 = input.next();
        System.out.println("请输入您要修改的原作者:");
        String author2 = input.next();
        preparedStatement.setString(1,author1);
        preparedStatement.setString(2,author2);
        int i = preparedStatement.executeUpdate();
        if(i == 1){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }else {
        PreparedStatement preparedStatement = connection.prepareStatement("update music set musicname = ? where musicname = ?");
        System.out.println("请输入您要修改的音乐名:");
        String musicname1 = input.next();
        System.out.println("请输入您要修改的原音乐名:");
        String musicname2 = input.next();
        preparedStatement.setString(1,musicname1);
        preparedStatement.setString(2,musicname2);
        int i = preparedStatement.executeUpdate();
        if(i == 1){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }
}

public static void delete(int id) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    connection = DBUtil.getConnection();
    int i = 0;
    try{
        statement = connection.prepareStatement("delete from music where id = ?");
        statement.setInt(1,id);
        i = statement.executeUpdate();
        if(i != 0){
            System.out.println("删除成功");

        }else{
            System.out.println("删除失败");
        }
    }catch (SQLException e){
        e.printStackTrace();
    }
}

}

DBUtil:
package com.zhongruan.until;

import java.sql.*;

public class DBUtil {
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456”);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}

public static void closeAll(ResultSet resultSet, PreparedStatement statement, Connection connection) {
    if (resultSet != null) {
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (statement != null){
        try{
            statement.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}

User:
package com.zhongruan.bean;

public class User {
private int id;
private String username;
private String password;
private int type;

public int getType() {
    return type;
}

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

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

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

public String getPassword() {
    return password;
}

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

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

}

Music:
package com.zhongruan.dao;

public class Music {
private int id;
private String musicname;
private String author;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getMusicname() {
    return musicname;
}

public void setMusicname(String musicname) {
    this.musicname = musicname;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

@Override
public String toString() {
    return "Music{" +
            "id=" + id +
            ", musicname='" + musicname + '\'' +
            ", author='" + author + '\'' +
            '}';
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值