package com.yihu.ehr.util.db; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.apache.commons.dbutils.BasicRowProcessor; import org.apache.commons.dbutils.BeanProcessor; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLXML; import java.sql.Timestamp; import java.util.Date; import java.util.HashMap; import java.util.Map; /** *
* BeanProcessor
matches column names to bean property names
* and converts ResultSet
columns into objects for those bean
* properties. Subclasses should override the methods in the processing chain
* to customize behavior.
*
*
* This class is thread-safe. *
* * @see BasicRowProcessor * @since DbUtils 1.1 */ public class BeanProcessorEx extends BeanProcessor { /** * Constructor for BeanProcessor. */ public BeanProcessorEx() { this(new HashMapResultSet
column into an object. Simple
* implementations could just call rs.getObject(index)
while
* more complex implementations could perform type manipulation to match
* the column's type to the bean property type.
* *
* This implementation calls the appropriate ResultSet
getter
* method for the given property type to perform the type conversion. If
* the property type doesn't match one of the supported
* ResultSet
types, getObject
is called.
*
ResultSet
currently being processed. It is
* positioned on a valid row before being passed into this method.
* @param index The current column index being processed.
* @param propType The bean property type that this column needs to be
* converted into.
* @return The object from the ResultSet
at the given column
* index after optional type processing or null
if the column
* value was SQL NULL.
* @throws SQLException if a database access error occurs
*/
protected Object processColumn(ResultSet rs, int index, Class> propType)
throws SQLException {
if (!propType.isPrimitive() && rs.getObject(index) == null) {
return null;
}
if (propType.equals(String.class)) {
return rs.getString(index);
} else if (
propType.equals(Integer.TYPE) || propType.equals(Integer.class)) {
return Integer.valueOf(rs.getInt(index));
} else if (
propType.equals(Boolean.TYPE) || propType.equals(Boolean.class)) {
return Boolean.valueOf(rs.getBoolean(index));
} else if (propType.equals(Long.TYPE) || propType.equals(Long.class)) {
return Long.valueOf(rs.getLong(index));
} else if (
propType.equals(Double.TYPE) || propType.equals(Double.class)) {
return Double.valueOf(rs.getDouble(index));
} else if (
propType.equals(Float.TYPE) || propType.equals(Float.class)) {
return Float.valueOf(rs.getFloat(index));
} else if (
propType.equals(Short.TYPE) || propType.equals(Short.class)) {
return Short.valueOf(rs.getShort(index));
} else if (propType.equals(Byte.TYPE) || propType.equals(Byte.class)) {
return Byte.valueOf(rs.getByte(index));
} else if (propType.equals(Timestamp.class)) {
return rs.getTimestamp(index);
} else if (propType.equals(SQLXML.class)) {
return rs.getSQLXML(index);
} else if (propType.equals(Date.class)) {
return new Timestamp(rs.getTimestamp(index).getTime());
} else {
return rs.getObject(index);
}
}
}