首页 > 解决方案 > Java MySQL 空结果集

问题描述

我正在尝试编写一个返回员工详细信息的小型 Java 应用程序。这是我的 Employee 类。

public class Employees {

private int id;
private Date dateofBirth;
private String firstName;
private String lastName;
private enum gender{
    M, F;
}
private gender employeeGender;
private Date dateHired;

public String getEmployeeGender() {
    return this.employeeGender.name();
}

public void setEmployeeGender(String employeeGender) {
    this.employeeGender = gender.valueOf(employeeGender);
}

/*Getters, setters omitted*/

这是我的 DAO 课程

public class EmployeeDao {

final String TABLE_EMPLOYEES = "employees";
final String COLUMN_EMPLOYEES_ID = "emp_no";
final String COLUMN_EMPLOYEES_DOB = "birth_date";
final String COLUMN_EMPLOYEES_FIRST_NAME = "first_name";
final String COLUMN_EMPLOYEES_LAST_NAME = "last_name";
final String COLUMN_EMPLOYEES_GENDER = "gender";
final String COLUMN_EMPLOYEES_HIRE_DATE = "hire_date";

final String QUERY_EMPLOYEES = "SELECT * FROM " + TABLE_EMPLOYEES + " WHERE " + COLUMN_EMPLOYEES_ID + " = ?";

public Employees getEmployeeDetails(int employeeId) {
    Employees employee = new Employees();
    try (DbConnection dbConnection = new DbConnection();
         Connection databaseConnection = dbConnection.getConn();
         PreparedStatement selectFromEmployees = databaseConnection.prepareStatement(QUERY_EMPLOYEES)) {
        selectFromEmployees.setInt(1, employeeId);
        try (ResultSet result = selectFromEmployees.executeQuery()) {

            if (result.next() == false) {
                System.out.println("Empty Resultset");
            }
            while (result.next()) {
                employee.setId(result.getInt(COLUMN_EMPLOYEES_ID));
                employee.setFirstName(result.getString(COLUMN_EMPLOYEES_FIRST_NAME));
                employee.setLastName(result.getString(COLUMN_EMPLOYEES_LAST_NAME));
                employee.setDateofBirth(result.getDate(COLUMN_EMPLOYEES_DOB));
                employee.setEmployeeGender(result.getString(COLUMN_EMPLOYEES_GENDER));
                employee.setDateHired(result.getDate(COLUMN_EMPLOYEES_HIRE_DATE));
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return employee;
}

}

但是当我尝试在这样的 main 方法中运行应用程序时,我得到一个带有空值的输出。

public static void main(String[] args) {
    EmployeeDao employeeDao = new EmployeeDao();
    Employees employees = employeeDao.getEmployeeDetails(39256);
    System.out.println(employees.getId() + " \n" + employees.getFirstName() + " \n" + employees.getLastName() + " \n" + employees.getDateofBirth() + " \n" + employees.getDateHired());
}

这是输出。 在此处输入图像描述

这是数据库中对应行的样子 在此处输入图像描述

标签: javamysqljdbc

解决方案


你不应该调用next两次,因为它会再次向前移动光标。尝试这个:

if (result.next() == false) {
    System.out.println("Empty Resultset");
} else {
    employee.setId(result.getInt(COLUMN_EMPLOYEES_ID));
    employee.setFirstName(result.getString(COLUMN_EMPLOYEES_FIRST_NAME));
    employee.setLastName(result.getString(COLUMN_EMPLOYEES_LAST_NAME));
    employee.setDateofBirth(result.getDate(COLUMN_EMPLOYEES_DOB));
    employee.setEmployeeGender(result.getString(COLUMN_EMPLOYEES_GENDER));
    employee.setDateHired(result.getDate(COLUMN_EMPLOYEES_HIRE_DATE));
}

推荐阅读