要写一个音乐管理系统总共要创建如下的文件夹与java文件
MusicDao的代码如下:
package com.zhongruan.dao;
import com.zhongruan.Util.DBUtil;
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 List<Music> findMusic(){
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
List<Music> musics=new ArrayList<>();
try {
connection= DBUtil.getConnection();
String sql="select * from music";
//4.得到statement
statement=connection.prepareStatement(sql);
//5.执行sql
resultSet=statement.executeQuery();
//6.处理结果集
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);
}
//7.关闭资源
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return musics;
}
public void delete(int id) throws SQLException,ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String sql="delete from music where id=?";
PreparedStatement statement =connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
public static void update(int id,String name,String author) throws SQLException,ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String sql="update music set name =?,author=? where id=?";
PreparedStatement statement =connection.prepareStatement(sql);
statement.setInt(3,id);
statement.setString(1,name);
statement.setString(2,author);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
public void insert(String name,String author) throws SQLException,ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String sql="insert into music(NAME,author) VALUES (?,?) ";
PreparedStatement statement =connection.