一、音乐管理系统的基本功能
1.新建了一个class,用来存放与数据库中表中的内容
代码:
public class Music {private int id;private String musicname;private String auto;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 getAuto() {return auto;}public void setAuto(String auto) {this.auto = auto;}@Overridepublic String toString() {return "Music{" +"id=" + id +", musicname='" + musicname + '\'' +", auto='" + auto + '\'' +'}';}
}
2.音乐管理系统的基本功能代码
查询代码:
public List<Music> findAll() throws SQLException {Connection connection= DBUtil.getConnection();String sql="select * from music";PreparedStatement statement=connection.prepareStatement(sql);ResultSet resultSet=statement.executeQuery();List<Music> musics=new ArrayList<>();while (resultSet.next()){int id = resultSet.getInt(1);String musicname = resultSet.getString(2);String write = resultSet.getString(3);Music music=new Music();music.setId(id);music.setMusicname(musicname);music.setAuto(write);musics.add(music);}return musics;}
添加代码
public void insert(Music music) throws SQLException {Connection connection=null;connection=DBUtil.getConnection();PreparedStatement statement=connection.prepareStatement("insert into music (musicname,auto) values (?,?)");statement.setString(1,music.getMusicname());statement.setString(2,music.getAuto());int re=statement.executeUpdate();DBUtil.close(connection,statement,null);if(re!=-1){System.out.println("添加成功!");}else{System.out.println("没有添加成功!");}}
删除代码:
public void update(Music music) throws SQLException {Connection connection=null;connection=DBUtil.getConnection();PreparedStatement statement=connection.prepareStatement("update music set musicname=? , auto=? where id=?");statement.setString(1,music.getMusicname());statement.setString(2,music.getAuto());statement.setInt(3,music.getId());int re=statement.executeUpdate();DBUtil.close(connection,statement,null);if(re!=-1){System.out.println("修改成功!");}else{System.out.println("没有修改成功!");}}
删除代码:
public void delete(String musicname) throws SQLException {Connection connection=DBUtil.getConnection();PreparedStatement statement=connection.prepareStatement("delete from music where musicname=?");statement.setString(1,musicname);int re=statement.executeUpdate();if(re!=-1){System.out.println("删除成功!");}else{System.out.println("没有删除成功!");}}
3.音乐管理系统的基本界面
代码:
public static void main(String[] args) throws SQLException, NoSuchAlgorithmException {Scanner input = new Scanner(System.in);UserDao userDao = new UserDao();MusicDao musicDao=new MusicDao();while (true) {System.out.print("请输入用户名:");String username = input.next();System.out.print("请输入密码:");String password=input.next();MessageDigest md=MessageDigest.getInstance("MD5");md.update(password.getBytes());String str=new BigInteger(1,md.digest()).toString(16);System.out.println("用户名"+username + "密码:"+str);boolean flag = userDao.loges(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.删除音乐");j=input.nextInt();switch (j){case 1:List<Music> musics=musicDao.findAll();System.out.println(musics);case 2:System.out.println("请输入要添加的歌名:");String name=input.next();System.out.println("请输入要添加的作者名:");String write=input.next();Music music=new Music();music.setMusicname(name);music.setAuto(write);musicDao.insert(music);case 3:System.out.println("请输入要修改的编号:");int id=input.nextInt();System.out.println("请输入要修改的歌名:");String name1=input.next();System.out.println("请输入要修改的作者名:");String auto=input.next();Music music1=new Music();music1.setMusicname(name1);music1.setAuto(auto);music1.setId(id);musicDao.update(music1);case 4:System.out.println("请输入要删除的歌名:");;musicDao.delete(input.next());}}}}