音乐系统完整版

说好要给完整版的
一份完整代码请签收
音乐系统完整版
这是布局图

下面首先是DBUtil.java

package com.zhongruan.util;

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”);
System.out.println(“创建连接成功”);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet resultSet, PreparedStatement preparedStatement,Connection connection){
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace( );
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace( );
}
}
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace( );
}
}
}
}

然后是Music.java

package com.zhongruan.beam;

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 + ‘’’ + ‘}’;
}
}

然后是User.java

package com.zhongruan.beam;

public class User {
private int id;
private String username;
private String password;
private int type;
public int getId(int id) {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName(String username) {
return this.username;
}
public void setName (String name){
this.username = name;
}
public String getPassword(String password){
return this.password;
}
public void setPassword (String password){
this.password = password;
}
public void settype(int type){ this.type=type; }
public int gettype(){ return type; }
@Override
public String toString() {
return “User{” + “id=” + id + “, username=’” + username + ‘’’ + “, password=’” + password + ‘’’ + ‘}’;
}

}

然后是MusicDao.java

package com.zhongruan.dao;

import com.zhongruan.beam.Music;
import com.zhongruan.util.DBUtil;

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

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 static int findtype(int id) throws SQLException {
Connection connection= DBUtil.getConnection();
PreparedStatement statement=connection.prepareStatement(“SELECT type FROM tb_user where id=?”);
ResultSet resultSet=statement.executeQuery();
resultSet.next();
int typ=resultSet.getInt(4);
return typ;
}
/
public static void Delete(int id) throws SQLException {
DBUtil dbUtil = new DBUtil( );
Connection connection = dbUtil.getConnection( );
MusicDao musicDao=new MusicDao();
PreparedStatement preparedStatement = connection.prepareStatement(“delete from music where id=?”);
preparedStatement.setInt(1,id);
int i=preparedStatement.executeUpdate();
if (i != 0/&& musicDao.findtype(id)==0/) {
System.out.println(“删除成功”);
} else {
System.out.println(“删除失败,只有管理员才有删除权限”);
}
}
public static void add(String musicname,String author) throws SQLException {
Connection connection = DBUtil.getConnection( );
String sql = “insert into music( musicname,author) values (?,?)”;
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,musicname);
preparedStatement.setString(2,author);
int resultSet = preparedStatement.executeUpdate( );
if (preparedStatement != null) {
System.out.println(“添加成功”);
}
}
public static void Update(String musicname,String author,int id) throws SQLException {
Connection connection=DBUtil.getConnection();
PreparedStatement statement=connection.prepareStatement(“update music set musicname=?, author=? where id=?”);
statement.setInt(3,id);
statement.setString(1,musicname);
statement.setString(2,author);
int i = statement.executeUpdate( );
if (i != 0) {
System.out.println(“修改成功”);
} else {
System.out.println(“修改失败”);
}
}

}

在然后是UserDao.java

package com.zhongruan.dao;

import com.zhongruan.beam.User;
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.Scanner;

public class UserDao {
public User 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();
User user=new User();
if(resultSet.next()) {
user.setId(resultSet.getInt(1));
user.setName(username);
user.setPassword(password);
user.settype(resultSet.getInt(4));
}
return user;
}
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.setString(3, String.valueOf(type));
int i = statement.executeUpdate();
if (i != 0) {
return true;
} else return false;
}
public ArrayList Uselect() throws SQLException {
Connection connection=DBUtil.getConnection();
String sql=“SELECT*FROM tb_user”;
ArrayList users=new ArrayList<>();
PreparedStatement preparedStatement=connection.prepareStatement(sql);
ResultSet resultSet=preparedStatement.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.setName(username);
user.setPassword(password);
users.add(user);
}
return users;
}
public void Uadd(String username,String password,int type) throws SQLException {
Connection connection=DBUtil.getConnection();
PreparedStatement preparedStatement=connection.prepareStatement(“INSERT INTO tb_user(username,password,type ) VALUES(?,?,?)”);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
preparedStatement.setInt(3,type);
int i=preparedStatement.executeUpdate();
if(preparedStatement!=null){
System.out.println(“添加成功!” );
}
}
public void Uupdate(int idd,String uusername,String ppssword,int type) throws SQLException {
Connection connection=DBUtil.getConnection();
PreparedStatement preparedStatement=connection.prepareStatement(“UPDATE tb_user SET type=?,username=?,password=? where id=?”);
preparedStatement.setInt(4,idd);
preparedStatement.setString(2,uusername);
preparedStatement.setString(3,ppssword);
preparedStatement.setInt(1,type);
int i=preparedStatement.executeUpdate();
if(i!=0){
System.out.println(“修改成功!” );
}else {
System.out.println(“修改失败!” );
}
}
public void Udelete(int id) throws SQLException {
Connection connection=DBUtil.getConnection();
PreparedStatement preparedStatement=connection.prepareStatement(“delete from tb_user where id=?”);
preparedStatement.setInt(1,id);
int i=preparedStatement.executeUpdate();
if (i!=0){
System.out.println(“删除成功!” );
}else {
System.out.println("删除失败! " );
}
}
}

最后就是业务层了Musicservice.java

package com.zhongruan.service;
import com.zhongruan.beam.Music;
import com.zhongruan.beam.User;
import com.zhongruan.dao.MusicDao;
import com.zhongruan.dao.UserDao;

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

public class MusicService {
List musics = new ArrayList<>( );
public static void main(String[] args) throws SQLException {
UserDao userDao = new UserDao();
MusicDao musicDao=new MusicDao();
MusicService musicService=new MusicService();
System.out.println("\t-----------请输入用户名-----------");
Scanner input = new Scanner(System.in);
String username = input.next();
System.out.println("\t------------请输入密码----------- -");
String password = input.next();
User user = userDao.login(username,password);
while (true) {
if (user!=null) {
musicService.getMenu(user);
} else {
musicService.zhuce();
}
}
}
public void getMenu(User user) throws SQLException {
Scanner input=new Scanner(System.in);
System.out.println("\t---------------登入成功----------------");
System.out.println("\t----------欢迎来到音乐管理系统----------\n");
System.out.println("\t1.音乐管理\t2.用户管理\t3.退出系统\t");
int j = input.nextInt() ;
if (j == 1) {
System.out.println("\t----------欢迎进入音乐管理系统----------");
System.out.println("\n\t1.查询音乐\t2.添加音乐\t3.修改音乐\t4.删除音乐\t5.返回上一层");
int a = input.nextInt( );
switch (a) {
case 1:
List musics = MusicDao.findAll( );
System.out.println(musics);
System.out.println(“按1退出系统\t按2返回上一级”);
int w=input.nextInt();
if(w1){
System.exit(0);
}else if(w
2){
getMenu(user);
}
break;
case 2:
if(user.gettype()!=0){
System.out.println(“你没有权限执行该操作” );
System.out.println(“返回上一级!” );
getMenu(user);
}
System.out.println(“请输入要添加的音乐名:”);
String nam = input.next( );
System.out.println(“请输入作者”);
String auth = input.next( );
Music music = new Music( );
music.setMusicname(nam);
music.setAuthor(auth);
MusicDao.add(nam, auth);
break;
case 3:
if(user.gettype()!=0){
System.out.println(“你没有权限执行该操作” );
System.out.println(“返回上一级!” );
getMenu(user);
}
System.out.println(MusicDao.findAll( ));
System.out.println(“请输入要修改音乐的id:”);
int idd = input.nextInt( );
System.out.println(“请输入修改后歌曲的名字”);
String musicname = input.next( );
System.out.println(“请输入修改后歌曲的作者”);
String author = input.next( );
MusicDao.Update(musicname, author, idd);
break;
case 4:
if(user.gettype()!=0){
System.out.println(“你没有权限执行该操作” );
System.out.println(“返回上一级!” );
getMenu(user);
}
System.out.println(“请输入要删除的歌曲的id”);
int id = input.nextInt( );
MusicDao.Delete(id);
break;
case 5:
getMenu(user);
break;
}
}else if (j2){
System.out.println("\t---------欢迎进入用户管理系统--------\n" );
System.out.println("\t1.查询用户\t2.添加用户\t3.修改用户\t4.删除用户\t5.返回上级菜单" );
UserDao userDao = new UserDao( );
int b=input.nextInt();
switch (b){
case 1:
List users=userDao.Uselect();
System.out.println(users);
break;
case 2:
System.out.println(“请输入你要添加的用户名:” );
String una=input.next();
System.out.println(“请设置密码:” );
String pass=input.next();
System.out.println(“请设置用户权限:” );
int typ = input . nextInt();
User use=new User();
use.setName(una);
use.setPassword(pass);
use.settype(typ);
userDao.Uadd(una,pass,typ);
break;
case 3:
System.out.println(“请输入要修改的用户的id:” );
int iid=input.nextInt();
System.out.println(“请输入要修改的用户名:” );
String uunam=input.next();
System.out.println(“请输入要修改的用户密码:” );
String pword=input.next();
System.out.println(“请输入要修改的用户权限:” );
int type=input.nextInt();
userDao.Uupdate(iid,uunam,pword,type);
break;
case 4:
System.out.println(“请输入要删除的用户的id:” );
int id=input.nextInt();
userDao.Udelete(id);
break;
case 5:
getMenu(user);
break;
}
}else if(j
3){
System.exit(0);
}
}
public void zhuce() throws SQLException {
UserDao userDao = new UserDao( );
Scanner input=new Scanner(System.in);
System.out.println("\t-----------登入失败------------");
System.out.println("\t-----------请先注册------------\n");
System.out.println(“请输入注册名”);
String zcm = input.next( );
System.out.println(“请输入注册密码”);
String zcmm1 = input.next( );
System.out.println(“请确认密码”);
String zcmm2 = input.next( );
System.out.println(“角色设定\tps:设为管理员请输入0,不设为管理员请输入1”);
int type = input.nextInt();
if (zcmm1.equals(zcmm2)&&(type0||type1)) {
userDao.zhuce(zcm, zcmm1,type);
System.out.println(“登录还是退出Y/N”);
System.out.print(" ");
String i = input.next( );
if (i.equals(“N”)) {
System.exit(0);
} else if (!i.equals(“Y”)) {
System.out.println(“请输入Y或N!”);
System.exit(0);
}else {
User user=new User();
getMenu(user);
}
} else {
System.out.println(“密码或角色有误,请重新注册!”);
}
}
}

以上就是一个星期以来的成果
白~