ElmDemo/src/main/java/dao/specification/userDAO.java

145 lines
4.6 KiB
Java

package dao.specification;
import dao.userAbstractDAO;
import entities.Users;
import org.jetbrains.annotations.NotNull;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import static util.SQLDatabaseConnection.getConnection;
import static util.SQLDatabaseConnection.close;
public class userDAO implements userAbstractDAO {
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
//新建用户
@Override
public int insert(@NotNull Users users) {
int flag;
try {
connection = getConnection();
String insert_sql = "insert into Users(name, phoneNumber, address, password) " +
"values (?,?,?,?)";
preparedStatement = connection.prepareStatement(insert_sql);
preparedStatement.setString(1, users.getName());
preparedStatement.setString(2, users.getPhoneNumber());
preparedStatement.setString(3, users.getAddress());
preparedStatement.setString(4, users.getPassword());
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(connection, preparedStatement, resultSet);
}
return flag;
}
//删除
@Override
public int delete(@NotNull Users users) {
int flag;
try {
connection = getConnection();
String delete_sql = "delete from Users where id=?";
preparedStatement = connection.prepareStatement(delete_sql);
preparedStatement.setLong(1, users.getId());
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(connection, preparedStatement, resultSet);
}
return flag;
}
//更新用户个人信息
@Override
public int update(@NotNull Users users) {
int flag;
String update_sql = "update Users " +
"set name = ?, phoneNumber = ?, address = ? " +
"where id = ?";
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(update_sql);
preparedStatement.setString(1, users.getName());
preparedStatement.setString(2, users.getPhoneNumber());
preparedStatement.setString(3, users.getAddress());
preparedStatement.setLong(4, users.getId());
flag = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return flag;
}
//查询所有用户
@Override
public List<Users> search(Users users) {
List<Users> list = new ArrayList<>();
String selectAll_sql = "select * from Users";
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(selectAll_sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
users = new Users();
users.setName(resultSet.getString("id"));
users.setName(resultSet.getString("name"));
users.setPhoneNumber(resultSet.getString("phoneNumber"));
users.setAddress(resultSet.getString("address"));
list.add(users);
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(connection, preparedStatement, resultSet);
}
return list;
}
//用ID查询用户
@Override
public Users searchID(Long id) {
Users user = null;
String selectID_sql = "select * from Users where id = ?";
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(selectID_sql);
preparedStatement.setLong(1, id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
user = new Users();
user.setId(resultSet.getLong("id"));
user.setName(resultSet.getString("name"));
user.setPhoneNumber(resultSet.getString("phoneNumber"));
user.setAddress(resultSet.getString("address"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(connection, preparedStatement, resultSet);
}
return user;
}
}