完善音乐管理系统:
package zhongruan.service;
import zhongruan.bean.Music;
import zhongruan.dao.MusicDao;
import zhongruan.dao.UserDao;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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();
MusicDao musicDao = new MusicDao();
while (true) {
System.out.println("- - - - - - -请输入用户名: - - - - - - -");
Scanner input = new Scanner(System.in);
String username = input.next();
System.out.println("- - - - - - -请输入密码: - - - - - - -");
String password = input.next();
System.out.println(username + password);
boolean flag = userDao.login(username, password);
if (flag) {
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.删除音乐");
}
int a = input.nextInt();
switch (a) {
case 1:
List<Music> musics = musicDao.findAll();
System.out.println(musics);
break;
case 2:
System.out.println("请输入要添加的音乐名称: ");
String n=input.next();
System.out.println("请输入歌手: ");
String au=input.next();
Music music=new Music();
music.setMusicname(n);
music.setAuthor(au);
musicDao.add(music);
break;
case 3:
case 4:
System.out.println("请输入要删除歌曲的id");
int id=input.nextInt();
musicDao.delete(id);
break;
}
} else {
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();
if (zcmm1.equals(zcmm2)) {
userDao.zhuce(zcm, zcmm1);
System.out.println("是否登入还是退出Y/N");
String i = input.next();
if (i.equals("N")) {
System.exit(0);
} else if (!i.equals("Y")) {
System.out.println("傻子,输错了! ! ! ! ");
System.exit(0);
}
} else {
System.out.println("密码有误,请重新注册! ");
}
}
}
}
}
MusicDao:
package zhongruan.dao;
import com.sun.org.apache.regexp.internal.RE;
import sun.rmi.log.LogInputStream;
import zhongruan.bean.Music;
import zhongruan.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MusicDao {
public 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 anthor=resultSet.getString(3);
Music music=new Music();
music.setId(id);
music.setMusicname(musicname);
music.setAuthor(anthor);
musics.add(music);
}
return musics;
}
public 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 (Exception e) {
e.printStackTrace();
}
}
public void add(Music music) throws SQLException {
Connection connection=DBUtil.getConnection();
PreparedStatement statement=
connection.prepareStatement("insert into music (musicname,author) values (?,?)");
statement.setString(1,"123");
statement.setString(2,"123");
statement.executeUpdate();
}
public void update(Music id1) throws SQLException {
Connection connection=DBUtil.getConnection();
int u=0;
PreparedStatement statement=
connection.prepareStatement("update music set (musicname,author) where id=?");
statement.setString(1,"c");
statement.setString(2,"d");
statement.setString(3,"id1");
u=statement.executeUpdate();
if (u!=0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
}
}