bean包
Music.java
public class Music {
private int id;
private String musicname;
private String author;
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Music{" +
"id=" + id +
", musicname='" + musicname + '\'' +
", author='" + author + '\'' +
'}';
}
}
User.java
public class User {
private int id;
private String username;
private String password;
private int type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", type=" + type +
'}';
}
}
dao包
MusicDao.java
import com.ningcai.bean.Music;
import com.ningcai.util.DBUtil;
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> 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;
}
public void delete(int id) throws SQLException{
PreparedStatement statement = null;
Connection connection = null;
connection = DBUtil.getConnection();
int i=0;
try {
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();
}
}
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();
}
UserDao.java
import com.ningcai.bean.User;
import com.ningcai.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class UserDao {
public boolean login(String username,String password) throws SQLException {
Connection connection = DBUtil.getConnection();
String sql="select * from tb_user where username=? and password=?";
PreparedStatement statement =connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
ResultSet resultSet = statement.executeQuery();
if(resultSet.next()){
return true;
}else
return false;
}
public boolean zhuce(String username,String password,int type) throws SQLException {
Connection connection = DBUtil.getConnection();
String sql="insert into tb_user(username,password,type) values(?,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
statement.setInt(3,type);
int i=statement.executeUpdate();
if(i!=0){
return true;
}else{
return false;
}
}
public List<User> findAll() throws SQLException {
List<User> users=new ArrayList<>();
Connection connection = DBUtil.getConnection();
PreparedStatement statement =connection.prepareStatement("select * from tb_user");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
int id=resultSet.getInt(1);
String username = resultSet.getString(2);
String password = resultSet.getString(3);
int type = resultSet.getInt(4);
User user=new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
user.setType(type);
users.add(user);
}
return users;
}
public void update(int id,String password) throws SQLException {
Connection connection = DBUtil.getConnection();
PreparedStatement statement = connection.prepareStatement("update tb_user set password=? where id=?");
statement.setString(1,password);
statement.setInt(2,id);
statement.executeUpdate();
}
}
service包
MusicService.java
import com.ningcai.bean.Music;
import com.ningcai.bean.User;
import com.ningcai.dao.MusicDao;
import com.ningcai.dao.UserDao;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class MusicService {
public static void main(String[] args) throws SQLException {
UserDao userDao = new UserDao();
MusicService musicService = new MusicService();
boolean flag = true;
while (flag) {
System.out.println("------------请输入用户名:------------");
Scanner input = new Scanner(System.in);
String username = input.next();
System.out.println("------------请输入密码:------------");
String password = input.next();
boolean a = userDao.login(username, password);
if (a) {
musicService.getMenu();
} else {
musicService.zhuce();
}
}
}
public void getMenu() throws SQLException {
MusicDao musicDao = new MusicDao();
UserDao userDao = new UserDao();
Scanner input = new Scanner(System.in);
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.删除音乐\t5.返回上一层");
int a = input.nextInt();
switch (a) {
case 1:
List<Music> musics = musicDao.findAll();
System.out.println(musics);
break;
case 2:
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;
case 3:
System.out.println("请输入要修改音乐的id:");
case 4:
System.out.println("请输入要删除歌曲的id:");
int id = input.nextInt();
musicDao.delete(id);
break;
case 5:
getMenu();
break;
}
}else if(j==2){
System.out.println("------------欢迎进入用户管理模块------------");
System.out.println("\t1.查询用户\t2.添加用户\t3.修改用户\t4.删除用户\t5.返回上一层");
int b = input.nextInt();
switch (b) {
case 1:
List<User> users = userDao.findAll();
System.out.println(users);
break;
case 2:
System.out.println("请输入注册用户名:");
String n = input.next();
System.out.println("请输入注册密码:");
String au = input.next();
System.out.println("请输入注册用户权限:");
int ty = input.nextInt();
User user = new User();
user.setUsername(n);
user.setPassword(au);
user.setType(ty);
userDao.zhuce(user.getUsername(),user.getPassword(),user.getType());
break;
case 3:
System.out.println("请输入要修改的id");
int id=input.nextInt();
System.out.println("请输入要修改后的密码:");
String password=input.next();
userDao.update(id,password);
break;
case 4:
case 5:
}
}
}
public void zhuce() throws SQLException{
UserDao userDao = new UserDao();
Scanner input = new Scanner(System.in);
System.out.println("------------登入失败------------");
System.out.println("------------请先注册------------");
System.out.println("请输入注册名:");
String zcm = input.next();
System.out.println("请输入注册密码:");
String zcmm1 = input.next();
System.out.println("请确认密码:");
String zcmm2 = input.next();
System.out.println("请输入角色:");
int js = input.nextInt();
if (zcmm1.equals(zcmm2) && (js==0 ||js==1)) {
userDao.zhuce(zcm,zcmm1,js);
System.out.println("是否登入还是退出Y/N");
String i = input.next();
if (i.equals("N")) {
System.exit(0);
} else if (!i.equals("Y")) {
System.out.println("撒子,输错了!!!!!");
System.exit(0);
}else {
getMenu();
}
} else {
System.out.println("密码有误,请重新注册");
}
}
}
util包
DBUtil.java
import java.sql.*;
public class DBUtil {
public static Connection getConnection(){
Connection connection=null;
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection= DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/nbcj?useSSL=true&characterEncoding=utf-8&user=root&password=123");
System.out.println("创建连接成功");
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(ResultSet rs, PreparedStatement preparedStatement, Connection connection) {
if (rs != null) {
try {
rs.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();
}
}
}
}
}