I use JDBC to enter and get classes in the database. In one of the classes, I use the enum value in the Status field.
Record:
private void writeOrder(List<Order> order) throws SQLException {
PreparedStatement pst = connection.prepareStatement("insert into \"Order\" (id,customerId,date,status,totalCost) values(?, ?, ?, ?, ?)");
for (Order a : order) {
pst.setObject(1, a.getId());
pst.setObject(2, a.getDate());
pst.setObject(3, a.getStatus());
pst.execute();
}
Reading:
private Object dataFromResultSet(ResultSet rs, Class aClass) throws SQLException{
return new Order(
rs.getLong(1),
rs.getTimestamp(2),
rs.getString(4),
);
}
When recording I get this error: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of ru.sfedu.SchoolMeals.model.OrderStatus. Use setObject() with an explicit Types value to specify the type to use.
But I suspect I did the reading wrong, too. How do I write and read a field of the enum class?