Dashboard > iBATIS DataMapper > Home > Type Handler Callbacks > UrlTypeHandlerCallback.java
UrlTypeHandlerCallback.java
Added by Ken Weiner, last edited by Ken Weiner on May 31, 2006
Labels: 


package com.yourpackage;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.sql.SQLException;
    import java.sql.Types;
    
    /**
    * An iBATIS type handler callback for java.net.URLs that are mapped to
    * Strings in the database. If a URL cannot be constructed based on the
    * String, then the URL will be set to <code>null</code>.
    * <p>
    * @author Ken Weiner
    */
    public class UrlTypeHandlerCallback implements TypeHandlerCallback {
    
    public Object getResult(ResultGetter getter) throws SQLException {
    String value = getter.getString();
    if (getter.wasNull()) {
    return null;
    }
    return this.valueOf(value);
    }
    
    public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
    if (parameter == null) {
    setter.setNull(Types.VARCHAR);
    } else {
    URL url = (URL) parameter;
    setter.setString(url.toExternalForm());
    }
    }
    
    public Object valueOf(String s) {
    URL url;
    try {
    url = new URL(s);
    } catch (MalformedURLException e) {
    url = null;
    }
    return url;
    }
    
    }

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators