先写好框架,框架有model层,dao层,util层,最后在view.java里写主程序。
model层
User.java
package com.zhongruan.model;
public class User {
private int id;
private String username;
private String password;
private int type;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
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 +
'}';
}
}
Music.java
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 + '\'' +
'}';
}
}
然后在util 里写DBUtil.java 来连接数据库
Util.java
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/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
return connection;
}
public static void ZYClose(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();
}
}
}
}
之后再到dao层里写增删改查的方法
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.sql.SQLException;
public class UserDao {
public static User selectDB(String username) throws SQLException, ClassNotFoundException {
Connection connection= DBUtil.getConnection();
String sql="select * from tb_user where username=?";
PreparedStatement statement=connection.prepareStatement(sql);
statement.setString(1,username);
ResultSet resultSet =statement.executeQuery();
User user=null;
while (resultSet.next()){
user=new User();
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
}
DBUtil.ZYClose(resultSet,statement,connection);
return user;
}
public static void insertDB(String username,String password,int i) throws SQLException, ClassNotFoundException {
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,i);
statement.executeUpdate();
DBUtil.ZYClose(null,statement,connection);
}
}
MusicDao.java
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 static List<Music> selectMusic() throws SQLException, ClassNotFoundException {
Connection connection=null;
PreparedStatement statement=null;
ResultSet resultSet=null;
List<Music> musics=new ArrayList<>();
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));
//System.out.println(music.toString());
musics.add(music);
}
DBUtil.ZYClose(resultSet,statement,connection);
return musics;
}
public static void insertMusic(String name,String author) throws SQLException, ClassNotFoundException {
Connection connection=null;
PreparedStatement statement=null;
connection=DBUtil.getConnection();
String sql="insert into music(id,name,author) values(null ,?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,name);
statement.setString(2,author);
statement.executeUpdate();
System.out.println("增加音乐成功");
DBUtil.ZYClose(null,statement,connection);
}
public static void deleteMusic(int id) throws SQLException, ClassNotFoundException {
Connection connection=null;
PreparedStatement statement=null;
connection=DBUtil.getConnection();
String sql="delete from music where id=?";
statement=connection.prepareStatement(sql);
statement.setInt(1,id);
statement.executeUpdate();
System.out.println("删除音乐成功");
DBUtil.ZYClose(null,statement,connection);
}
public static void updateMusic(int id,String newname,String author ) throws SQLException, ClassNotFoundException {
Connection connection=null;
PreparedStatement statement=null;
connection=DBUtil.getConnection();
String sql="update music set name=?,author=? where id=?";
statement=connection.prepareStatement(sql);
statement.setString(1,newname);
statement.setString(2,author);
statement.setInt(3,id);
statement.executeUpdate();
System.out.println("更新音乐成功");
DBUtil.ZYClose(null,statement,connection);
}
}
最后在写view
view.java
package com.zhongruan;
import com.zhongruan.dao.MusicDao;
import com.zhongruan.dao.UserDao;
import com.zhongruan.model.Music;
import com.zhongruan.model.User;
import javax.security.auth.login.LoginException;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class View {
public static void caseMusic(boolean b) throws SQLException, ClassNotFoundException {
Scanner input=new Scanner(System.in);
System.out.println("1.查询音乐 2.增加音乐 3.删除音乐 4.更新音乐 5.退出登录");
int n=input.nextInt();
switch (n){
case 1:
List<Music> list=MusicDao.selectMusic();
System.out.println(list);
break;
case 2:
System.out.println("请输入要增加音乐的名字:");
String name=input.next();
System.out.println("请输入要增加音乐的作作者:");
String author=input.next();
MusicDao.insertMusic(name,author);
break;
case 3:
System.out.println("请输入要删除音乐的id:");
int id3=input.nextInt();
MusicDao.deleteMusic(id3);
break;
case 4:
System.out.println("请输入要修改音乐的id");
int id4=input.nextInt();
System.out.println("请输入新的歌名");
String newname4=input.next();
System.out.println("请输入新的作者");
String newauthor4=input.next();
MusicDao.updateMusic(id4,newname4,newauthor4);
break;
case 5:
b=false;
break;
default:
System.out.println("输入数字错误,请重新输入");
}
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
Scanner input=new Scanner(System.in);
boolean b=true;
while (b){
System.out.println("请输入你的账号:");
String username=input.next();
System.out.println("请输入你的密码:");
String password=input.next();
User user=UserDao.selectDB(username);
if (user==null){
System.out.println("账号不存在,请注册");
System.out.println("请输入你要注册的用户名");
String newname=input.next();
System.out.println("请输入你的密码");
String newpassword=input.next();
UserDao.insertDB(newname,newpassword,1);
}else if (user.getPassword().equals(password)){
System.out.println("登录成功,欢迎来到音乐管理系统");
caseMusic(b);
}else {
//System.out.println("密码错误,请重新输入");
try {
throw new LoginException("密码错误,请重新输入");
} catch (LoginException e) {
e.printStackTrace();
}
}
}
}
}