结构图
View.java
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.ArrayList;
import java.util.List;
import java.util.Scanner;
public class View {
public static void main(String[] args) throws LoginException, SQLException, ClassNotFoundException {
boolean flag=true;
while (flag==true){
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 newusername=input.next();
System.out.println("-----请输入密码:-----");
String newpassword=input.next();
userDao.zhuce(newusername,newpassword);
}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 {
System.out.println("1.音乐查询 2.音乐添加 3.音乐修改 4.音乐删除 5.退出");
Scanner input=new Scanner(System.in);
int choice=input.nextInt();
MusicDao musicDao=new MusicDao();
List<Music> musics=new ArrayList<>();
switch (choice){
case 1:
musics=musicDao.findmusics();
break;
case 2:
if (user.getType()!=0){
System.out.println("权限不足,请联系管理员");
break;
}else{
System.out.println("请输入新音乐名:");
String name=input.next();
System.out.println("请输入新音乐作者:");
String auther=input.next();
musicDao.insert(name,auther);
musics=musicDao.findmusics();
System.out.println(musics);
break;
}
case 3:
if (user.getType()!=0){
System.out.println("权限不足,请联系管理员");
break;
}else{
System.out.println("请输入修改音乐id:");
Integer id1=input.nextInt();
System.out.println("请输入修改音乐名:");
String name1=input.next();
System.out.println("请输入修改音乐作者:");
String auther1=input.next();
musicDao.updata(id1,name1,auther1);
musics=musicDao.findmusics();
System.out.println(musics);
break;
}
case 4:
if (user.getType()!=0){
System.out.println("权限不足,请联系管理员");
break;
}else{
System.out.println("请输入删除音乐id:");
Integer id2=input.nextInt();
musicDao.delete(id2);
musics=musicDao.findmusics();
System.out.println(musics);
break;
}
case 5:
System.exit(0);
}
}
}
MusicDao.java
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 static List<Music> findmusics(){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=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.setAuther(resultSet.getString(3));
musics.add(music);
}
System.out.println(musics);
}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 void insert(String name,String auther) throws SQLException,ClassNotFoundException{
Connection connection=DBUtil.getconnection();
String sql="insert into music(name,auther) values(?,?)";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,name);
statement.setString(2,auther);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
public void updata(int id,String name,String auther) throws SQLException,ClassNotFoundException{
Connection connection=DBUtil.getconnection();
String sql="UPDATE music SET name=?,auther=? WHERE id=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setInt(3,id);
statement.setString(1,name);
statement.setString(2,auther);
statement.executeUpdate();
DBUtil.closeAll(null,statement,connection);
}
}
UserDao.java
package com.zhongruan.dao;
import com.zhongruan.model.User;
import com.zhongruan.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public User findUserByUsername(String username){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=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 =new User();
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
user.setType(resultSet.getInt(4));
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return user;
}
public void zhuce(String username,String password){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
User user =new User();
try{
connection= DBUtil.getconnection();
String sql="insert into tb__user (username,password) values(?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
statement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
}
}
LoginException.java
package com.zhongruan.dao;
import com.zhongruan.model.User;
import com.zhongruan.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public User findUserByUsername(String username){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=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 =new User();
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
user.setType(resultSet.getInt(4));
}
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
return user;
}
public void zhuce(String username,String password){
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
User user =new User();
try{
connection= DBUtil.getconnection();
String sql="insert into tb__user (username,password) values(?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
statement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
}
}
Music.java
package com.zhongruan.model;
public class Music {
private int id;
private String name;
private String auther;
public int getId(int anInt) {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName(String string) {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuther(String string) {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
@Override
public String toString() {
return "Music{" +
"id=" + id +
", name='" + name + '\'' +
", auther='" + auther + '\'' +
'}';
}
}
User.java
package com.zhongruan.model;
public class User {
private Integer id;
private String username;
private String password;
private Integer type;
public Integer getId() {
return id;
}
public void setId(Integer 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 Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", type=" + type +
'}';
}
}
DBUtil.java
package com.zhongruan.util;
import java.awt.dnd.DragGestureEvent;
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/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
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();
}
}
}
}