增加了用户管理系统
package dao;
import Utill.DBUtill;
import bean.Music;
import bean.User;
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 boolean login(String username, String password) throws Exception {
Connection connection = DBUtill.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()) {
return true;
} else {
return false;
}
}
public static boolean zhuce(String username, String password,int type) throws Exception {
Connection connection = DBUtill.getconnection();
String sql = "insert into tb_user(username,password,type ) value (?,?,?)";
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 static List<User> findAll() throws Exception {
List<User> users=new ArrayList<>();
Connection connection = DBUtill.getconnection();
PreparedStatement statement=connection.prepareStatement("select * from tb_user");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
int id=resultSet.getInt(1);
String username=resultSet.getString(2);
String password=resultSet.getString(3);
int type=resultSet.getInt(4);
User user=new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
users.add(user);
}
return users;
}
public void update(int id2,String mm1) throws Exception {
Connection connection=DBUtill.getconnection();
PreparedStatement statement=null;
String sql="update tb_user set password=? where id=?";
statement=connection.prepareStatement(sql);
statement.setString(1,mm1);
statement.setInt(2,id2);
int i=0;
i=statement.executeUpdate();
if (i==1){
System.out.println("修改成功");
}else {
System.out.println("失败了");
}
}
}