首先新建三个类分别命名为 User,Userdao和view
先编写User,代码如下:
public class User {
private int id;
private String username;
private String 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserDao的代码为:
import com.gongmao.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public static User findUserByUsername(String username) throws SQLException {
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=new User();
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 churu(String username,String passwod) throws SQLException {
ResultSet resultSet=null;
PreparedStatement statement=null;
Connection connection=null;
try {
connection=DBUtil.getConnection();
String sql="insert into tb_user(username,password) values(?,?)";
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtil.closeAll(resultSet,statement,connection);
}
}
}
然后编写主程序view,在view中添加登录页面,设置用户名和密码,如果没有则要新注册一个用户名,代码如下:
public class View {
public static void main(String[] args) throws LoginException, SQLException, ClassNotFoundException {
boolean flag = true;
while (flag) {
Scanner input = new Scanner(System.in);
System.out.println(“请输入用户名”);
String username = input.next();
System.out.println(“请输入密码”);
String passwod = input.next();
UserDao userDao = new UserDao();
User user = Us![在这里插入图片描述](https://img-blog.csdnimg.cn/20190918155628927.PNG)erDao.findUserByUsername(username);
if (user == null) {
System.out.println("---该用户尚未注册。请先注册---");
System.out.println("请输入注册名");
String name = input.next();
System.out.println("请输入注册码");
String pw = input.next();
userDao.churu(name, pw);
写完后运行结果如下:
如果是新用户的话则为
然后去数据库中新建一个表,命名为tb_user,设置id,username,password,type四个变量
然后在数据库中添加几条信息,回view主程序编写查询代码
else if (user.getPassword().equals(passwod)) {
System.out.println("—欢迎来到音乐管理系统—");
musicSystem();
flag=false;
} else {
//System.out.println("—密码输入错误,请重新输入");
throw new LoginException(“登入失败”);
}
}
}
public static void musicSystem() throws SQLException, ClassNotFoundException {
Scanner input=new Scanner(System.in);
System.out.println("1.音乐查询 2.音乐添加 3.音乐删除 4.音乐修改");
int choice=input.nextInt();
switch (choice){
case 1:
//1.音乐查询
List<Music> musics=MusicDao.findMusics();
System.out.println(musics);
break;
case 2:
添加完成后运行,结果就可以显示在数据库中添加的几条信息了
第二步为音乐添加,代码如下:
然后在运行结果中添加一首歌和作者
然后再去数据库中查询刚才添加的记录,截图如下
第三步为音乐删除,编写删除代码
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.updateMusic(id1,newym,newau);
break;
运行结果如下
然后去数据库中查询结果
显示为修改成功。