音乐管理系统搭建2
import univ.nbcj.bean.Music;
import univ.nbcj.service.User;
import univ.nbcj.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
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 boolean login(String username, String password, int type) throws SQLException {
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()) {
return true;
} else {
return false;
}
}
public static List<User> findAll() throws SQLException {
List<User> users = new ArrayList<>();
Connection connection = DBUtil.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);
User user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
users.add(user);
}
return users;
}
public static void add(User user) throws SQLException{
Connection connection=DBUtil.getconnection();
PreparedStatement statement= connection.prepareStatement
("insert into tb_user(username,password,type) value (?,?,?)");
statement.setString(1,"123");
statement.setString(2,"123");
statement.setInt(3,1);
}
}