增删查

标题java日记

day9
建立一个音乐管理系统,主要学习增删改查的内容

创建一个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");

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } 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();
        }
    }

}

查

    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;
}

case 1:
List musics=musicDao.findall();
System.out.println(" ");


public void delete(int id) throws SQLException {
Connection connection = null;
PreparedStatement preparedStatement = null;
connection = DBUtil.getConnection();
int i = 0;
try {
PreparedStatement 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 (SQLException e) {
    e.printStackTrace();
}

}

case 2:
System.out.println(“请输入要删除歌曲的id”);
int id=input.nextInt();
musicDao.delete(id);


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());
statement.executeUpdate();

case 3:
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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值