|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
A simple interface for implementing custom type handlers.
Using this interface, you can implement a type handler that will perform customized processing before parameters are set on a PreparedStatement and after values are retrieved from a ResultSet. Using a custom type handler you can extend the framework to handle types that are not supported, or handle supported types in a different way. For example, you might use a custom type handler to implement proprietary BLOB support (e.g. Oracle), or you might use it to handle booleans using "Y" and "N" instead of the more typical 0/1. EXAMPLEHere's a simple example of a boolean handler that uses "Yes" and "No".
public class YesNoBoolTypeHandlerCallback implements TypeHandlerCallback { private static final String YES = "Yes"; private static final String NO = "No"; public Object getResult(ResultGetter getter) throws SQLException { String s = getter.getString(); if (YES.equalsIgnoreCase(s)) { return new Boolean (true); } else if (NO.equalsIgnoreCase(s)) { return new Boolean (false); } else { throw new SQLException ("Unexpected value " + s + " found where "+YES+" or "+NO+" was expected."); } } public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { boolean b = ((Boolean)parameter).booleanValue(); if (b) { setter.setString(YES); } else { setter.setString(NO); } } public Object valueOf(String s) { if (YES.equalsIgnoreCase(s)) { return new Boolean (true); } else if (NO.equalsIgnoreCase(s)) { return new Boolean (false); } else { throw new SQLException ("Unexpected value " + s + " found where "+YES+" or "+NO+" was expected."); } } }
Method Summary | |
java.lang.Object |
getResult(ResultGetter getter)
Performs processing on a value before after it has been retrieved from a ResultSet. |
void |
setParameter(ParameterSetter setter,
java.lang.Object parameter)
Performs processing on a value before it is used to set the parameter of a PreparedStatement. |
java.lang.Object |
valueOf(java.lang.String s)
Casts the string representation of a value into a type recognized by this type handler. |
Method Detail |
public void setParameter(ParameterSetter setter, java.lang.Object parameter) throws java.sql.SQLException
setter
- The interface for setting the value on the PreparedStatement.parameter
- The value to be set.
java.sql.SQLException
- If any error occurs.public java.lang.Object getResult(ResultGetter getter) throws java.sql.SQLException
getter
- The interface for getting the value from the ResultSet.
java.sql.SQLException
- If any error occurs.public java.lang.Object valueOf(java.lang.String s)
s
- A string representation of a valid value for this type.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |