java 音乐管理系统案例(一)

mvc
m:model 实体类
v:view 视图
c:controller 控制

包结构
model 实体类
dao 持久层 (对数据库进行操作和增删改查)

java 音乐管理系统

类Test1
package com.zhongruan;

import com.zhongruan.dao.MusicDao;
import com.zhongruan.model.Music;

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

public class Test1 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
MusicDao musicDao=new MusicDao();
/System.out.println(“请输入音乐名:”);/
Scanner input=new Scanner(System.in);
/* String name=input.next();
System.out.println(“请输入作者:”);
String author=input.next();
musicDao.charu(name,author);
List musics=musicDao.findMusics();
System.out.println(musics);*/

  /*  System.out.println("请输入你要删除的音乐id:");
    int id=input.nextInt();*/
    System.out.println("请输入你要修改的id:");
    int id1=input.nextInt();
    String huche=input.nextLine();
    System.out.println("请输入新的歌名:");
    String name1=input.nextLine();
    System.out.println("请输入新的作者名:");
    String author1=input.nextLine();
    MusicDao.updateMusic(id1,name1,author1);
    List<Music> musics=musicDao.findMusics();
    System.out.println(musics);


    /*try {
        musicDao.delete(1);
    } catch (SQLException e){
        e.printStackTrace();
    } catch (ClassNotFoundException e){
        e.printStackTrace();
    }*/
}

}

类DBUtill
package com.zhongruan;

import java.sql.*;

public class DBUtill {
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName(“com.mysql.jdbc.Driver”);
Connection connection= DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/zjgm?useSSL=true&characterEncoding=utf-8&user=root&password=123456”);
return connection;
}
public static void closeAll(ResultSet resultSet, Statement 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 (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

类MusicDao
package com.zhongruan.dao;

import com.zhongruan.DBUtill;
import com.zhongruan.Test;
import com.zhongruan.model.Music;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MusicDao {
public static void updataMusic(int id1, String newym, String newau) {
}

public List<Music> findMusics() {
    ResultSet resultSet = null;
    PreparedStatement statement = null;
    Connection connection = null;
    List<Music> musics = new ArrayList<>();
    try {
        connection = DBUtill.getConnection();
        String sql = "select * from music";
        statement = connection.prepareStatement(sql);
        resultSet = statement.executeQuery();

        while (resultSet.next()) {
            Music music = new Music();
            music.setId(resultSet.getInt(1));
            music.setName(resultSet.getString(2));
            music.setAuthor(resultSet.getString(3));
            musics.add(music);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DBUtill.closeAll(resultSet, statement, connection);
    }
    return musics;

}

public void delete(int id) throws SQLException, ClassNotFoundException {
    Connection connection = DBUtill.getConnection();
    String sql = "delete from music where id=?";
    PreparedStatement statement = connection.prepareStatement(sql);
    statement.setInt(1, id);
    statement.executeUpdate();
    DBUtill.closeAll(null, statement, connection);
}
public void charu(String name,String author){
    ResultSet resultSet=null;
    PreparedStatement statement=null;
    Connection connection=null;
    try{
        connection=DBUtill.getConnection();
        String sql="insert into music(name,author) values (?,?)";
        statement=connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setString(2,author);
        statement.executeUpdate();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        DBUtill.closeAll(resultSet,statement,connection);
    }
}
public static void updateMusic(int id,String newname,String author) throws SQLException,ClassNotFoundException{
    Connection connection=DBUtill.getConnection();
    String sql="update music set name=? ,author=? where id=?";

    PreparedStatement statement=connection.prepareStatement(sql);
    statement.setString(1,"hualili");
    statement.setString(2,"hualili");
    statement.setInt(3,id);
    statement.executeUpdate();
    DBUtill.closeAll(null,statement,connection);

}

}

类Music
package com.zhongruan.model;

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

public int getId(){
    return id;
}
public void setId(int id){
    this.id=id;
}
public String getName(){
    return name;
}
public void setName(String name){
    this.name=name;
}
public String getAuthor(){
    return author;
}
public void setAuthor(String author){
    this.author=author;
}
@Override
public String toString(){
    return "Music{"+
            "id"+id+
            ",name='"+name+'\''+
            ",author'"+author+'\''+
            '}';
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值