0

Why is my result set in Java reading my DBeaver column for date as null and how can I work around this?

Below you can see photos showing that Java is reading my date column from DBeaver as null when that is not the case, as well as my prepared statement and result set in Java.

Java console - can see that Java is reading my date column as null when it is not

DBeaver - can see that my date column is not null

public Set<Reimbursement> getReimbursementsForEmployee(Employee employee) {
        Connection conn = ConnectionUtil.getConnection();
        String sql = "SELECT * FROM reimbursement WHERE e_id = ?";

        Set<Reimbursement> reimbursements = new HashSet<Reimbursement>();

        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1, employee.getEmployeeId());
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Reimbursement reimbursement = new Reimbursement();
                reimbursement.setReimbursementNumber(rs.getInt("reimbursementnumber"));
                reimbursement.setDate(rs.getDate("ddate"));
                reimbursement.setAmount(rs.getDouble("amount"));
                reimbursement.setReimbursementRequestInfo(rs.getString("reimbursementrequestinfo"));
                reimbursement.setManagerComment(rs.getString("managercomment"));
                reimbursement.setStatus(rs.getString("status"));
                reimbursement.setEmployeeId(rs.getInt("e_id")); 
                reimbursement.setManagerId(rs.getInt("m_id"));

                reimbursements.add(reimbursement);
            }

            return reimbursements;

        } catch (SQLException e) {

            e.printStackTrace();

        }
        return null;
    }
saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
  • Try using rs.getString("ddate"), and then convert to the Date object using 'SimpleDateFormat'. I think the conversion is not working, and hence returning null. – trim24 Jan 25 '20 at 13:57
  • thank you. I gave that a try, but Java is still reading the date as null. reimbursement.setDate(rs.getString(simpleDateFormat.format("ddate"))); – dlaz Jan 25 '20 at 14:17
  • Which RDBMS is under your DBeaver? (Like Oracle, MariaDB or DB2, for example.) Which datatype is your column? `date`? – Ole V.V. Jan 25 '20 at 14:27
  • 1
    MariaDB. In Java my date datatype is LocalDate. In MariaDB, my date column was created with the following syntax: ddate date not null default CURRENT_DATE, – dlaz Jan 25 '20 at 14:34
  • It’s probably not answering your question, but if `reimbursement.setDate()` expects a `LocalDate` (which is good), you should use `rs.getObject("ddate", LocalDate.class)` (provided JDBC 4.2). [Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2](https://stackoverflow.com/questions/43039614/insert-fetch-java-time-localdate-objects-to-from-an-sql-database-such-as-h2) – Ole V.V. Jan 25 '20 at 14:39
  • 2
    amazing! thank you so much to everyone for your help! rs.getObject("ddate", LocalDate.class) works! – dlaz Jan 25 '20 at 14:45

0 Answers0