音乐管理系统搭建3
package com.app.dao;
import com.app.bean.User;
import com.app.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UserDao {
public boolean register(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 int login(String username, String password) throws SQLException {
int type=0;
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()){
type=resultSet.getInt(4);
}
return type;
}
public List<User> select() throws SQLException {
List<User> user = new ArrayList<>();
Scanner input = new Scanner(System.in);
String username, password;
int id, type;
System.out.println("请输入id");
id = input.nextInt();
Connection connection = DBUtil.getconnection();
PreparedStatement statement = connection.prepareStatement("select * from tb_user where id=?");
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
id = resultSet.getInt(1);
username = resultSet.getString(2);
password = resultSet.getString(3);
type = resultSet.getInt(4);
User user1 = new User();
user1.setId(id);
user1.setUsername(username);
user1.setPassword(password);
user.add(user1);
}
return user;
}
public boolean modify() throws SQLException {
Connection connection = DBUtil.getconnection();
Scanner input = new Scanner(System.in);
int id;
id = input.nextInt();
PreparedStatement statement = connection.prepareStatement("UPDATE tb_user SET username=\"bbb\" WHERE id =?");
statement.setInt(1,id);
int result=0;
result = statement.executeUpdate();
if (result != 0) {
return true;
} else {
return false;
}
}
public boolean del() throws SQLException {
Connection connection=DBUtil.getconnection();
Scanner input=new Scanner(System.in);
int id;
System.out.println("输入id:");
id=input.nextInt();
PreparedStatement statement=connection.prepareStatement("DELETE FROM tb_USER WHERE id=?");
statement.setInt(1,id);
int result=0;
result=statement.executeUpdate();
if(result!=0){
return true;
}else{
return false;
}
}
}