1.文件目录及文件如下
2.dao层
MusicDao类
package com.zhongruan.dao;
import com.zhongruan.model.Music;
import com.zhongruan.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> findMusic() {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<Music> 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, ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String sql = "delete from music where id=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate();
}
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.setString(1,name);
statement.setString(2,author);
statement.setInt(3,id);
statement.executeUpdate();
}
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.prepareStatement(sql);
statement.setString(1,name);
statement.setString(2,author);
statement.executeUpdate();
}
}
UserDao类
package com.zhongruan.dao;
import com.zhongruan.model.Music;
import com.zhongruan.model.User;
import com.zhongruan.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 UserDao {
public User finduserByusername(String username){
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
User user=null;
try {
connection = DBUtil.getConnection();
String sql = “select * from tb_user where username=?”;
statement = connection.prepareStatement(sql);
statement.setString(1,username);
resultSet = statement.executeQuery();
while (resultSet.next()) {
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeALL(resultSet, statement, connection);
}
return user;
}
public void insert(String username,String password) throws SQLException, ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String sql = "insert into tb_user(username,password) VALUES (?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
statement.executeUpdate();
}
public void delete(String username) throws SQLException, ClassNotFoundException {
Connection connection = DBUtil.getConnection();
String sql = "delete from tb_user where username=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.executeUpdate();
}
public 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.setString(1,name);
statement.setString(2,author);
statement.setInt(3,id);
statement.executeUpdate();
}
}
3.exception类
package com.zhongruan.exception;
public class LoginException extends Exception {
public LoginException(String message){
super(message);
}
}
4.model层
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 + '\'' +
'}';
}
}
User
在这里插入代码片
package com.zhongruan.model;
public class User {
public User(String username,String password){
this.username=username;
this.password=password;
}
private int id;
private String username;
private String password;
private int type;
public int getType() {
return type;
}
public User(){
}
public void setType(int type) {
this.type = 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", type=" + type +
'}';
}
}
5.DButil
package com.zhongruan.util;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/zjgm?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();
}
}
}
}
6.主体类
package com.zhongruan;
import com.zhongruan.dao.MusicDao;
import com.zhongruan.dao.UserDao;
import com.zhongruan.exception.LoginException;
import com.zhongruan.model.Music;
import com.zhongruan.model.User;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class View {
public static void main(String[] args) throws SQLException, LoginException, ClassNotFoundException {
boolean flag = true;
while (flag) {
Scanner input = new Scanner(System.in);
System.out.println("您尚未登入请先登入");
System.out.println("请输入用户名:");
String username = input.next();
System.out.println("请输入密码:");
String password = input.next();
UserDao userDao = new UserDao();
User user =userDao.finduserByusername(username);
if (user == null) {
System.out.println("---该用户尚未注册,请先注册---");
System.out.println("---请输入注册名:---");
String name = input.next();
System.out.println("---请输入注册密码:---");
String pw = input.next();
userDao.insert(name, pw);
break;
} else if (user.getPassword().equals(password)) {
System.out.println("----欢迎来到音乐管理系统----");
musicSystem(user);
flag = false;
} else {
//System.out.println("-----密码错误,请重新输入-----");
throw new LoginException("登入失败");
}
}
}
public static void musicSystem(User user) throws SQLException, ClassNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("1.音乐查询 2.音乐添加 3.音乐删除 4.音乐修改 5.退出 ");
int choice = input.nextInt();
MusicDao musicDao = new MusicDao();
switch (choice) {
case 1:
List<Music> musics = musicDao.findMusic();
System.out.println(musics);
case 2:
System.out.println("请输入要插入的音乐名: ");
String ym = input.next();
System.out.println("请输入插入的作者名: ");
String au = input.next();
musicDao.insert(ym, au);
break;
case 3:
System.out.println("请输入要删除的id:");
int id = input.nextInt();
musicDao.delete(id);
case 4:
System.out.println("请输入要修改的id:");
int id1 = input.nextInt();
System.out.println("请输入新的音乐名:");
String newym = input.next();
System.out.println("请输入新的作者名:");
String newau = input.next();
MusicDao.update(id1, newym, newau);
break;
case 5:
System.exit(0);
}
}
}