bean包中:
名为Music的class类
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的class类
public class User {
private int id;
private String username;
private String password;
private String 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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", type='" + type + '\'' +
'}';
}
}
dao包
名为MusicDao的class类
public class MusicDao {
public List<Music> findAll() throws SQLException {
List<Music> 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 delete(int id) throws SQLException {
PreparedStatement statement=null;
Connection connection=null;
connection=DBUtil.getConnection();
int i=0;
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("================删除失败================");
}
}
public void add(Music music) throws SQLException {
Connection connection=DBUtil.getConnection();
PreparedStatement statement=connection.prepareStatement
("insert into music (musicname,author) values (?,?)");
statement.setString(1,music.getMusicname());
statement.setString(2,music.getAuthor());
int i=statement.executeUpdate();
if (i!=0){
System.out.println("添加成功!");
}else{
System.out.println("添加失败!");
}
}
}
名为UserDao的class类
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 boolean zhuce (String username,String password,String 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,type);
int i=statement.executeUpdate();
if (i!=0){
return true;
}else
return false;
}
/*public List<User> findAll() throws SQLException {
List<User> users=new ArrayList<>();
Connection connection= DBUtil.getConnection();
PreparedStatement statement=connection.prepareStatement("select * from tb_user");
ResultSet resultSet=statement.executeQuery();
while (resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password=resultSet.getString(3);
String type=resultSet.getString(4);
User user=new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setType(type);
users.add(user);
}
return users;
}*/
}
service包中
名为MusicService的class类
public class MusicService {
public static void main(String[] args) throws SQLException {
UserDao userDao = new UserDao();
MusicService musicService = new MusicService();
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = input.next();
System.out.println("请输入密码:");
String password = input.next();
boolean flag = userDao.login(username, password);
if (flag) {
musicService.getMenu();
} else {
System.out.println("================登录失败================");
System.out.println("================请先注册================");
System.out.println("是否注册Y/N:");
String k = input.next();
if (k.equals("Y")) {
musicService.zhuce();
} else if (k.equals("N")) {
System.out.println("ヾ( ̄▽ ̄)Bye~Bye~!");
System.exit(0);
} else {
System.out.println("================操作错误================");
System.exit(0);
}
}
}
}
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("1.查询音乐\t2.用户管理\t3.退出系统");
int k = input.nextInt();
if (k == 1) {
System.out.println("========欢迎进入音乐管理系统========");
System.out.println("1.查询音乐\t2.添加音乐\t3.修改音乐\t4.删除音乐\t5.返回上一級");
int choice = input.nextInt();
switch (choice) {
case 1:
List<Music> musics = musicDao.findAll();
System.out.println(musics);
break;
case 2:
System.out.println("请输入要添加的音乐名称:");
Scanner input1 = new Scanner(System.in);
String n = input1.nextLine();
System.out.println("请输入作者:");
String m = input1.nextLine();
Music music = new Music();
music.setMusicname(n);
music.setAuthor(m);
musicDao.add(music);
break;
case 3:
case 4:
System.out.println("请输入要删除的歌曲id:");
int id = input.nextInt();
musicDao.delete(id);
break;
case 5:
System.out.println("ヾ( ̄▽ ̄)Bye~Bye~!");
break;
}
}
}
public void zhuce() throws SQLException {
UserDao userDao = new UserDao();
Scanner input = new Scanner(System.in);
System.out.println("请输入注册用户名:");
String zcm = input.next();
System.out.println("请输入注册用户的密码:");
String zcmm1 = input.next();
System.out.println("请确认注册用户的密码:");
String zcmm2 = input.next();
System.out.println("请输入注册用户的权限:");
String js = input.next();
if (zcmm1.equals(zcmm2) && (js.equals("0") || js .equals("1"))) {
userDao.zhuce(zcm, zcmm1, js);
System.out.println("================注册成功================");
System.out.println("是否登录Y/N:");
String choice = input.next();
if (choice.equals("N")) {
System.exit(0);
} else if (choice.equals("Y")) {
getMenu();
} else {
System.out.println("================操作错误================");
System.exit(0);
}
} else {
System.out.println("两次输入的密码不一致或注册格式输入错误,是否重新注册Y/N:");
String w = input.next();
if (w.equals("Y")) {
zhuce();
} else if (w.equals("N")) {
System.out.println("ヾ( ̄▽ ̄)Bye~Bye~!");
System.exit(0);
} else {
System.out.println("================操作错误================");
System.exit(0);
}
}
}
}
util包中
名为DBUtil的class类
public class DBUtil {
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection=DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}