简体   繁体   English

Java MySQL空结果集

[英]Java MySQL null resultset

I'm trying to write a small java application that returns the details for an employee. 我正在尝试编写一个小的Java应用程序,该应用程序返回员工的详细信息。 Here's my Employee class. 这是我的员工班。

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*/

Here's my DAO class 这是我的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;
}

} }

But when I try to run the app in main method like this, I get an output with null values. 但是,当我尝试以这种主要方法运行应用程序时,会得到带有空值的输出。

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());
}

This is the output. 这是输出。 在此处输入图片说明

This is how the corresponding row looks like in the database 这就是数据库中相应行的样子 在此处输入图片说明

You should not call next twice, since it will move the cursor forward again. 你不应该叫next两次,因为它会再次向前移动光标。 Try this: 尝试这个:

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));
}

Calling ResultSet#next moves the cursor forward a row, so your if condition loses the first row. 调用ResultSet#next将光标向前移动一行,因此if条件将丢失第一行。 Since you know your query can return at most one row, you don't need the while loop at all, however: 因为您知道查询最多可以返回一行,所以您根本不需要while循环,但是:

public Employees getEmployeeDetails(int employeeId) throws SQLException {
    Employees employee = null;
    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()) {
                employee = new Employees();
                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));
            }
        }
    }
    return employee;
}

No need to add extra result.next() comparison. 无需添加额外的result.next()比较。

if (result.next() == false) {
 System.out.println("Empty Resultset");
}
while (result.next()){

}

while will execute only if there are any rows. while仅在有任何行时执行。

Check the size of list generated before using to check if it contains value or not. 在用于检查列表中是否包含值之前,请检查所生成列表的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM