Skip to content

Instantly share code, notes, and snippets.

@twogood
Created June 13, 2017 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twogood/cf4517a42cd62a97207ee571372f0776 to your computer and use it in GitHub Desktop.
Save twogood/cf4517a42cd62a97207ee571372f0776 to your computer and use it in GitHub Desktop.
ArgumentFactory to support LocalDate with @BindBean in JDBI
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.time.LocalDate;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.Argument;
import org.skife.jdbi.v2.tweak.ArgumentFactory;
public class LocalDateArgumentFactory implements ArgumentFactory<LocalDate> {
@Override
public boolean accepts(Class<?> expectedType, Object value, StatementContext ctx) {
return value instanceof LocalDate;
}
@Override
public Argument build(Class<?> expectedType, LocalDate value, StatementContext ctx) {
return new Argument() {
@Override
public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException {
if (value == null) {
statement.setNull(position, Types.DATE);
} else {
statement.setDate(position, Date.valueOf(value));
}
}
};
}
}
@twogood
Copy link
Author

twogood commented Jun 13, 2017

jdbi.registerArgumentFactory(new LocalDateArgumentFactory());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment