音乐管理系统修改版
作者:互联网
今天的代码在昨天的基础上做了修改
1.新建java class文件MusicDao,并新建Dao包
2.test1代码如下
package com.zhongruan;
import com.zhongruan.Dao.MusicDao;
//import com.zhongruan.dao.MusicDao;
import com.zhongruan.model.Music;
import java.sql.*;
import java.util.List;
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
MusicDao musicDao=new MusicDao();
List musics=musicDao.findMusics();
System.out.println(musics);
Scanner input=new Scanner(System.in);
System.out.println(“请输入你喜欢的歌名:”);
String name=input.next();
System.out.println(“请输入你喜欢的歌的歌手:”);
String author=input.next();
System.out.println(“请输入你想修改的歌的id:”);
int ids=input.nextInt();
System.out.println(“请输入你想修改歌的歌名:”);
String names=input.next();
System.out.println(“请输入你想删除歌的id:”);
int id=input.nextInt();
try {
musicDao.zj(name,author);
} catch (SQLException e) {
e.printStackTrace();
}
try {
musicDao.xg(ids,names);
} catch (SQLException e) {
e.printStackTrace();
}
try {
musicDao.delete(id);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
MusicDao代码如下
package com.zhongruan.Dao;
import com.zhongruan.model.Music;
import com.zhongruan.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MusicDao {
public List findMusics() {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List musics=new ArrayList<>();
try {
connection = DBUtil.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 {
DBUtil.closeAll(resultSet, statement, connection);
}
return musics;
}
public void delete(int id) throws SQLException {
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 void zj(String name,String author) throws SQLException {
Connection connection = DBUtil.getConnection();
String sql = “insert into music(name,author) values (?,?)”;//增加数据
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setString(2,author);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
public void xg(int id,String name) throws SQLException {
Connection connection = DBUtil.getConnection();
String sql = “update music set name=? where id=?”;//更改数据
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(2,id);
statement.setString(1,name);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
}
DBUtil代码如下
package com.zhongruan.util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection(){
try {
Class.forName(“com.mysql.jdbc.Driver”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection= null;
try {
connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/zjgm?characterEncoding=utf-8&user=root&password=123456”);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(“操作成功!”);
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement, Connection connection){
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
最后运行test1,结果如下
数据库原来数据
运行后数据库数据
标签:String,管理系统,音乐,connection,修改版,statement,sql,import,DBUtil 来源: https://blog.csdn.net/kou_13/article/details/100924683