Java Reference
In-Depth Information
Imagine we're mapping this table to a class as the following:
public class User {
private int id;
private String username;
private String passwordHashcode;
private boolean enabled;
// assume compliant JavaBeans properties
// (getters/setters) below
Notice the mismatch between data types here. In the database, the Enabled col-
umn is a VARCHAR storing YES and NO values, whereas our Java class is a boolean
type. We can't directly set a value of YES or NO to a boolean type. Therefore, we
need to translate it. It's certainly possible that a JDBC driver could do this for us,
but let's assume that is not the case.
The purpose of a TypeHandlerCallback is to deal with these situations. So let's
write the implementation that would do just that.
Creating a TypeHandlerCallback
As we've seen, the TypeHandlerCallback interface is simple. All we need to do is
create a class that implements the interface. We'll give the new class a nice,
descriptive name, and also include a couple of private constants:
public class YesNoTypeHandlerCallback
implements TypeHandlerCallback {
private static final String YES = "YES";
private static final String NO = "NO";
public void setParameter(
ParameterSetter setter, Object parameter)
throws SQLException {
public Object getResult(ResultGetter getter)
throws SQLException {
public Object valueOf(String s) {
This is just a skeletal implementation of a type handler; in the following sections
we'll flesh it out.
Search WWH ::

Custom Search