Skip to content

Instantly share code, notes, and snippets.

@mageddo
Created May 29, 2018 18:34
Show Gist options
  • Select an option

  • Save mageddo/be0e19441591ddac36287607cd2b5f61 to your computer and use it in GitHub Desktop.

Select an option

Save mageddo/be0e19441591ddac36287607cd2b5f61 to your computer and use it in GitHub Desktop.
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;
public class JdbcHelper {
public static Integer getInteger(ResultSet rs, String label) throws SQLException {
return rs.getObject(label, Integer.class);
}
public static LocalDateTime getLocalDateTime(ResultSet rs, String label) throws SQLException {
return Optional.ofNullable(rs.getTimestamp(label))
.map(Timestamp::toLocalDateTime)
.orElse(null);
}
public static LocalDate getLocalDate(ResultSet rs, String label) throws SQLException {
return Optional.ofNullable(rs.getDate(label))
.map(Date::toLocalDate)
.orElse(null);
}
public static void setInteger(PreparedStatement ps, int offset, Integer value) throws SQLException {
ps.setObject(offset, value, Types.INTEGER);
}
public static void setLong(PreparedStatement ps, int offset, Long value) throws SQLException {
ps.setObject(offset, value, Types.BIGINT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment