首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java MySQL null结果集

Java MySQL null结果集
EN

Stack Overflow用户
提问于 2018-07-08 01:40:50
回答 3查看 60关注 0票数 1

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

代码语言:javascript
复制
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类

代码语言:javascript
复制
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方法中运行应用程序时,我得到了一个空值的输出。

代码语言:javascript
复制
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());
}

这是输出。

这是相应行在数据库中的外观

EN

回答 3

Stack Overflow用户

发布于 2018-07-08 01:44:17

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

代码语言:javascript
复制
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));
}
票数 4
EN

Stack Overflow用户

发布于 2018-07-08 01:46:31

调用ResultSet#next会将游标向前移动一行,因此if条件将丢失第一行。因为您知道查询最多只能返回一行,所以根本不需要while循环:

代码语言:javascript
复制
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;
}
票数 2
EN

Stack Overflow用户

发布于 2018-07-08 01:47:12

不需要添加额外的result.next()比较。

代码语言:javascript
复制
if (result.next() == false) {
 System.out.println("Empty Resultset");
}
while (result.next()){

}

while将仅在有任何行的情况下执行。

使用前检查生成的列表大小,检查是否有值。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51225539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档