当前位置: 首页 > 编程日记 > 正文

BeanUtils威力和代价

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

BeanUtils: 威力和代价(转载综合)

Apache Jakarta Commons项目非常有用。我曾在许多不同的项目上或直接或间接地使用各种流行的commons组件。其中的一个强大的组件就是BeanUtils。我将说明如何使用BeanUtils将local实体bean转换为对应的value 对象:


BeanUtils.copyProperties(aValue, aLocal)

上面的代码从aLocal对象复制属性到aValue对象。它相当简单!它不管local(或对应的value)对象有多少个属性,只管进行复制。我们假设local对象有100个属性。上面的代码使我们可以无需键入至少100行的冗长、容易出错和反复的get和set方法调用。这太棒了!太强大了!太有用了!

现在,还有一个坏消息:使用BeanUtils的成本惊人地昂贵!我做了一个简单的测试,BeanUtils所花费的时间要超过取数据、将其复制到对应的value对象(通过手动调用get和set方法),以及通过串行化将其返回到远程的客户机的时间总和。所以要小心使用这种威力!

  • 如果您有BeanUtils和类似的实用程序方面的体验,请与我交流分享。
    +prakash

Beanutils用了魔术般的反射技术,实现了很多夸张有用的功能,都是C/C++时代不敢想的。无论谁的项目,始终一天都会用得上它。我算是后知后觉了,第一回看到它的时候居然错过。

1.属性的动态getter,setter

在这框架满天飞的年代,不能事事都保证执行getter,setter函数了,有时候属性是要需要根据名字动态取得的,就像这样:
BeanUtils.getProperty(myBean,"code");
而BeanUtils更强的功能是直接访问内嵌对象的属性,只要使用点号分隔。
BeanUtils.getProperty(orderBean, "address.city");
相比之下其他类库的BeanUtils通常都很简单,不能访问内嵌的对象,所以经常要用Commons BeanUtils替换它们。
BeanUtils还支持List和Map类型的属性。如下面的语法即可取得顾客列表中第一个顾客的名字
BeanUtils.getProperty(orderBean, "customers[1].name");
其中BeanUtils会使用ConvertUtils类把字符串转为Bean属性的真正类型,方便从HttpServletRequest等对象中提取bean,或者把bean输出到页面。
而PropertyUtils就会原色的保留Bean原来的类型。

2.beanCompartor 动态排序

还是通过反射,动态设定Bean按照哪个属性来排序,而不再需要在bean的Compare接口进行复杂的条件判断。
List peoples = ...; // Person对象的列表
Collections.sort(peoples, new BeanComparator("age"));

如果要支持多个属性的复合排序,如"Order By lastName,firstName"

ArrayList sortFields = new ArrayList();
sortFields.add(new BeanComparator("lastName"));
sortFields.add(new BeanComparator("firstName"));
ComparatorChain multiSort = new ComparatorChain(sortFields);
Collections.sort(rows,multiSort);

其中ComparatorChain属于jakata commons-collections包。
如果age属性不是普通类型,构造函数需要再传入一个comparator对象为age变量排序。
另外, BeanCompartor本身的ComparebleComparator, 遇到属性为null就会抛出异常, 也不能设定升序还是降序。
这个时候又要借助commons-collections包的ComparatorUtils.

Comparator mycmp = ComparableComparator.getInstance();
mycmp = ComparatorUtils.nullLowComparator(mycmp); //允许null
mycmp = ComparatorUtils.reversedComparator(mycmp); //逆序
Comparator cmp = new BeanComparator(sortColumn, mycmp);

3.Converter 把Request或ResultSet中的字符串绑定到对象的属性

经常要从request,resultSet等对象取出值来赋入bean中,下面的代码谁都写腻了,如果不用MVC框架的绑定功能的话。

String a = request.getParameter("a");bean.setA(a);String b = ....

不妨写一个Binder:

MyBean bean = ...;HashMap map = new HashMap();Enumeration names = request.getParameterNames();while (names.hasMoreElements()){String name = (String) names.nextElement();map.put(name, request.getParameterValues(name));}BeanUtils.populate(bean, map);

其中BeanUtils的populate方法或者getProperty,setProperty方法其实都会调用convert进行转换。
但Converter只支持一些基本的类型,甚至连java.util.Date类型也不支持。而且它比较笨的一个地方是当遇到不认识的类型时,居然会抛出异常来。
对于Date类型,我参考它的sqldate类型实现了一个Converter,而且添加了一个设置日期格式的函数。
要把这个Converter注册,需要如下语句:

ConvertUtilsBean convertUtils = new ConvertUtilsBean();DateConverter dateConverter = new DateConverter();convertUtils.register(dateConverter,Date.class);//因为要注册converter,所以不能再使用BeanUtils的静态方法了,必须创建BeanUtilsBean实例BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtils,new PropertyUtilsBean());beanUtils.setProperty(bean, name, value);

4 其他功能

4.1 PropertyUtils,当属性为Collection,Map时的动态读取:
Collection: 提供index
BeanUtils.getIndexedProperty(orderBean,"items",1);
或者
BeanUtils.getIndexedProperty(orderBean,"items[1]");

Map: 提供Key Value
BeanUtils.getMappedProperty(orderBean, "items","111");//key-value goods_no=111
或者
BeanUtils.getMappedProperty(orderBean, "items(111)")
4.2 PropertyUtils,获取属性的Class类型
public static Class getPropertyType(Object bean, String name)
4.3 ConstructorUtils,动态创建对象
public static Object invokeConstructor(Class klass, Object arg)
4.4 MethodUtils,动态调用方法
MethodUtils.invokeMethod(bean, methodName, parameter);
4.5 动态Bean 见 用DynaBean减除不必要的VO和FormBean
一、概述
第一次看到BeanUtils包,是在Struts项目中,作为Struts一个工具来使用的,
估计功能越弄越强,就移到Common项目中了吧。

BeanUtils一共有四个package:
org.apache.commons.beanutils
org.apache.commons.beanutils.converters
org.apache.commons.beanutils.locale
org.apache.commons.beanutils.locale.converters
后三个包主要是用于数据的转换,围绕着一个Converter接口,该接口只有一个方法:
java.lang.Object convert(java.lang.Class type, java.lang.Object value) ,
用于将一个value转换成另一个类型为type的Object。在一些自动化的应用中应该会有用。
这里不作评论,以后有兴趣了,或者觉得有用了,再行研究。
这里只讲第一个包。

二、测试用的Bean
在开始所有的测试之前,我写了一个简单的Bean,以便于测试,代码如下:
package test.jakarta.commons.beanutils;

/**
* @author SonyMusic
*
*/
public class Month {
private int value;
private String name;
private int[] days={11,22,33,44,55};

public Month(int v, String n){
value=v;
name=n;
}

/**
* Returns the name.
* @return String
*/
public String getName() {
return name;
}

/**
* Returns the value.
* @return int
*/
public int getValue() {
return value;
}

/**
* Sets the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* Sets the value.
* @param value The value to set
*/
public void setValue(int value) {
this.value = value;
}

/**
* @see java.lang.Object#toString()
*/
public String toString() {
return value+"("+name+")";
}

public int[] getDays() {
return days;
}

public void setDays(int[] is) {
days = is;
}

}

三、BeanUtils
这是一个主要应用于Bean的Util(呵呵,这个解释很绝吧),以下是其中几个方法的例子

//static java.util.Map describe(java.lang.Object bean)
//这个方法返回一个Object中所有的可读属性,并将属性名/属性值放入一个Map中,另外还有
//一个名为class的属性,属性值是Object的类名,事实上class是java.lang.Object的一个属性
Month month=new Month(1, "Jan");

try {
Map map=BeanUtils.describe(month);
Set keySet=map.keySet();
for (Iterator iter = keySet.iterator(); iter.hasNext();) {
Object element = (Object) iter.next();
System.out.println("KeyClass:"+element.getClass().getName());
System.out.println("ValueClass:"+map.get(element).getClass().getName());
System.out.print(element+"\t");
System.out.print(map.get(element));
System.out.println();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
输出为:
KeyClass:java.lang.String
ValueClass:java.lang.String
value 1
KeyClass:java.lang.String
ValueClass:java.lang.String
class class test.jakarta.commons.beanutils.Month
KeyClass:java.lang.String
ValueClass:java.lang.String
name Jan
注意到所有Map中的key/value都是String,而不管object中实际的值是多少。
与此对应的还有static void populate(java.lang.Object bean, java.util.Map properties)
用于将刚才describe出的Map再装配成一个对象。


再看这样一段代码
曹晓钢也许还记得,为了取一个不确定对象的property,着实花了不少时间,
难度不大,但要做到100%的正确,仍然需要付出很大的精力。
//static java.lang.String getProperty(java.lang.Object bean, java.lang.String name)
Month month=new Month(1, "Jan");

try {
System.out.println(BeanUtils.getProperty(month,"value"));
} catch (Exception e) {
e.printStackTrace();
}
//输出是:1

与getProperty类似的还有getIndexedProperty, getMappedProperty,
以getIndexedProperty为例:
Month month=new Month(1, "Jan");

try {
System.out.println(BeanUtils.getIndexedProperty(month,"days",1));
System.out.println(BeanUtils.getIndexedProperty(month,"days[1]"));
} catch (Exception e) {
e.printStackTrace();
}
这两个调用是相同的。


BeanUtils中还有一个方法:
static void copyProperties(java.lang.Object dest, java.lang.Object orig)
它真是太有用,还记得struts中满天飞的都是copyProperties,我甚至怀疑整个BeanUtils最初
是不是因为这个方法的需求才写出来的。
它将对象orig中的属性复制到dest中去。


四、PropertyUtils
这个类和BeanUtils类很多的方法在参数上都是相同的,但返回值不同。
BeanUtils着重于"Bean",返回值通常是String,而PropertyUtils着重于属性,
它的返回值通常是Object。


五、ConstructorUtils
这个类中的方法主要分成两种,一种是得到构造方法,一种是创建对象。
事实上多数时候得到构造方法的目的就是创建对象,这里只介绍一下创建对象。
//static java.lang.Object ConstructorUtils.invokeConstructor
//(java.lang.Class klass, java.lang.Object[] args)
//根据一个java.lang.Class以及相应的构造方法的参数,创建一个对象。
Object obj=ConstructorUtils.invokeConstructor(Month.class, {new Integer(1), "Jan"});
Month month=(Month)obj;
try {
System.out.println(BeanUtils.getProperty(month,"value"));
} catch (Exception e) {
e.printStackTrace();
}
输出证明,构造方法的调用是成功的。
如果需要强制指定构造方法的参数类型,可以这样调用:
Object[] args={new Integer(1), "Jan"};
Class[] argsType={int.class, String.class};
Object obj;
obj = ConstructorUtils.invokeExactConstructor(Month.class, args, argsType);
Month month=(Month)obj;
System.out.println(BeanUtils.getProperty(month,"value"));
argsType指定了参数的类型。
六、ConstructorUtils补遗
创建对象还有一个方法:invokeExactConstructor,该方法对参数要求
更加严格,传递进去的参数必须严格符合构造方法的参数列表。
例如:
Object[] args={new Integer(1), "Jan"};
Class[] argsType={int.class, String.class};
Object obj;
//下面这句调用将不会成功,因为args[0]的类型为Integer,而不是int
//obj = ConstructorUtils.invokeExactConstructor(Month.class, args);

//这一句就可以,因为argsType指定了类型。
obj = ConstructorUtils.invokeExactConstructor(Month.class, args, argsType);
Month month=(Month)obj;
System.out.println(BeanUtils.getProperty(month,"value"));


七、MethodUtils
与ConstructorUtils类似,不过调用的时候,通常需要再指定一个method name的参数。

八、DynaClass/DynaBean
这似乎是BeanUtils中最有趣的部分之一了,很简单,简单到光看这两个接口中的方法会不明白
为什么要设计这两个接口。不过看到ResultSetDynaClass后,就明白了。下面是java doc中的代码:
ResultSet rs = ...;
ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);
Iterator rows = rsdc.iterator();
while (rows.hasNext()) {
DynaBean row = (DynaBean) rows.next();
... process this row ...
}
rs.close();
原来这是一个ResultSet的包装器,ResultSetDynaClass实现了DynaClass,它的iterator方法返回一个
ResultSetIterator,则是实现了DynaBean接口。
在获得一个DynaBean之后,我们就可以用
DynaBean row = (DynaBean) rows.next();
System.out.println(row.get("field1")); //field1是其中一个字段的名字

再看另一个类RowSetDynaClass的用法,代码如下:
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/2hu?useUnicode=true&characterEncoding=GBK";
String username="root";
String password="";

java.sql.Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url);
ps=con.prepareStatement("select * from forumlist");
rs=ps.executeQuery();
//先打印一下,用于检验后面的结果。
while(rs.next()){
System.out.println(rs.getString("name"));
}
rs.beforeFirst();//这里必须用beforeFirst,因为RowSetDynaClass只从当前位置向前滚动

RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
ps.close();
List rows = rsdc.getRows();//返回一个标准的List,存放的是DynaBean
for (int i = 0; i <rows.size(); i++) {
DynaBean b=(DynaBean)rows.get(i);
System.out.println(b.get("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
con.close();
} catch (Exception e) {
}
}

是不是很有趣?封装了ResultSet的数据,代价是占用内存。如果一个表有10万条记录,rsdc.getRows()
就会返回10万个记录。@_@

需要注意的是ResultSetDynaClass和RowSetDynaClass的不同之处:
1,ResultSetDynaClass是基于Iterator的,一次只返回一条记录,而RowSetDynaClass是基于
List的,一次性返回全部记录。直接影响是在数据比较多时ResultSetDynaClass会比较的快速,
而RowSetDynaClass需要将ResultSet中的全部数据都读出来(并存储在其内部),会占用过多的
内存,并且速度也会比较慢。
2,ResultSetDynaClass一次只处理一条记录,在处理完成之前,ResultSet不可以关闭。
3,ResultSetIterator的next()方法返回的DynaBean其实是指向其内部的一个固定
对象,在每次next()之后,内部的值都会被改变。这样做的目的是节约内存,如果你需要保存每
次生成的DynaBean,就需要创建另一个DynaBean,并将数据复制过去,下面也是java doc中的代码:
ArrayList results = new ArrayList(); // To hold copied list
ResultSetDynaClass rsdc = ...;
DynaProperty properties[] = rsdc.getDynaProperties();
BasicDynaClass bdc =
new BasicDynaClass("foo", BasicDynaBean.class,
rsdc.getDynaProperties());
Iterator rows = rsdc.iterator();
while (rows.hasNext()) {
DynaBean oldRow = (DynaBean) rows.next();
DynaBean newRow = bdc.newInstance();
PropertyUtils.copyProperties(newRow, oldRow);
results.add(newRow);
}

事实上DynaClass/DynaBean可以用于很多地方,存储各种类型的数据。自己想吧。嘿嘿。


九、自定义的CustomRowSetDynaClass
两年前写过一个与RowSetDynaClass目标相同的类,不过多一个功能,就是分页,只取需要的数据,
这样内存占用就会减少。

先看一段代码:
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/2hu?useUnicode=true&characterEncoding=GBK";
String username="root";
String password="";

java.sql.Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url);
ps=con.prepareStatement("select * from forumlist order by name");
rs=ps.executeQuery();
/*
while(rs.next()){
System.out.println(rs.getString("name"));
}
rs.beforeFirst();
*/

//第二个参数表示第几页,第三个参数表示页的大小
CustomRowSetDynaClass rsdc = new CustomRowSetDynaClass(rs, 2, 5);
//RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
ps.close();
List rows = rsdc.getRows();
for (int i = 0; i <rows.size(); i++) {
DynaBean b=(DynaBean)rows.get(i);
System.out.println(b.get("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
con.close();
} catch (Exception e) {
}
}
在这里用到了一个CustomRowSetDynaClass类,构造方法中增加了page和pageSize两个参数,
这样,不管数据库里有多少条记录,最多只取pageSize条记录,若pageSize==-1,则功能和
RowSetDynaClass一样。这在大多数情况下是适用的。该类的代码如下:

package test.jakarta.commons.beanutils;

import java.io.*;
import java.sql.*;
import java.util.*;

import org.apache.commons.beanutils.*;

/**
* @author SonyMusic
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class CustomRowSetDynaClass implements DynaClass, Serializable {

// ----------------------------------------------------------- Constructors

/**
* <p>Construct a new { @link RowSetDynaClass} for the specified
* <code>ResultSet</code>. The property names corresponding
* to column names in the result set will be lower cased.</p>
*
* @param resultSet The result set to be wrapped
*
* @exception NullPointerException if <code>resultSet</code>
* is <code>null</code>
* @exception SQLException if the metadata for this result set
* cannot be introspected
*/
public CustomRowSetDynaClass(ResultSet resultSet) throws SQLException {

this(resultSet, true);

}

/**
* <p>Construct a new { @link RowSetDynaClass} for the specified
* <code>ResultSet</code>. The property names corresponding
* to the column names in the result set will be lower cased or not,
* depending on the specified <code>lowerCase</code> value.</p>
*
* <p><strong>WARNING</strong> - If you specify <code>false</code>
* for <code>lowerCase</code>, the returned property names will
* exactly match the column names returned by your JDBC driver.
* Because different drivers might return column names in different
* cases, the property names seen by your application will vary
* depending on which JDBC driver you are using.</p>
*
* @param resultSet The result set to be wrapped
* @param lowerCase Should property names be lower cased?
*
* @exception NullPointerException if <code>resultSet</code>
* is <code>null</code>
* @exception SQLException if the metadata for this result set
* cannot be introspected
*/
public CustomRowSetDynaClass(ResultSet resultSet, boolean lowerCase)
throws SQLException {

this(resultSet, 1, -1, lowerCase);

}

public CustomRowSetDynaClass(
ResultSet resultSet,
int page,
int pageSize,
boolean lowerCase)
throws SQLException {

if (resultSet == null) {
throw new NullPointerException();
}
this.lowerCase = lowerCase;
this.page = page;
this.pageSize = pageSize;

introspect(resultSet);
copy(resultSet);

}

public CustomRowSetDynaClass(ResultSet resultSet, int page, int pageSize)
throws SQLException {
this(resultSet, page, pageSize, true);
}

// ----------------------------------------------------- Instance Variables

/**
* <p>Flag defining whether column names should be lower cased when
* converted to property names.</p>
*/
protected boolean lowerCase = true;

protected int page = 1;
protected int pageSize = -1;

/**
* <p>The set of dynamic properties that are part of this
* { @link DynaClass}.</p>
*/
protected DynaProperty properties[] = null;

/**
* <p>The set of dynamic properties that are part of this
* { @link DynaClass}, keyed by the property name. Individual descriptor
* instances will be the same instances as those in the
* <code>properties</code> list.</p>
*/
protected Map propertiesMap = new HashMap();

/**
* <p>The list of { @link DynaBean}s representing the contents of
* the original <code>ResultSet</code> on which this
* { @link RowSetDynaClass} was based.</p>
*/
protected List rows = new ArrayList();

// ------------------------------------------------------ DynaClass Methods

/**
* <p>Return the name of this DynaClass (analogous to the
* <code>getName()</code> method of <code>java.lang.Class</code), which
* allows the same <code>DynaClass</code> implementation class to support
* different dynamic classes, with different sets of properties.</p>
*/
public String getName() {

return (this.getClass().getName());

}

/**
* <p>Return a property descriptor for the specified property, if it
* exists; otherwise, return <code>null</code>.</p>
*
* @param name Name of the dynamic property for which a descriptor
* is requested
*
* @exception IllegalArgumentException if no property name is specified
*/
public DynaProperty getDynaProperty(String name) {

if (name == null) {
throw new IllegalArgumentException("No property name specified");
}
return ((DynaProperty) propertiesMap.get(name));

}

/**
* <p>Return an array of <code>ProperyDescriptors</code> for the properties
* currently defined in this DynaClass. If no properties are defined, a
* zero-length array will be returned.</p>
*/
public DynaProperty[] getDynaProperties() {

return (properties);

}

/**
* <p>Instantiate and return a new DynaBean instance, associated
* with this DynaClass. <strong>NOTE</strong> - This operation is not
* supported, and throws an exception.</p>
*
* @exception IllegalAccessException if the Class or the appropriate
* constructor is not accessible
* @exception InstantiationException if this Class represents an abstract
* class, an array class, a primitive type, or void; or if instantiation
* fails for some other reason
*/
public DynaBean newInstance()
throws IllegalAccessException, InstantiationException {

throw new UnsupportedOperationException("newInstance() not supported");

}

// --------------------------------------------------------- Public Methods

/**
* <p>Return a <code>List</code> containing the { @link DynaBean}s that
* represent the contents of each <code>Row</code> from the
* <code>ResultSet</code> that was the basis of this
* { @link RowSetDynaClass} instance. These { @link DynaBean}s are
* disconnected from the database itself, so there is no problem with
* modifying the contents of the list, or the values of the properties
* of these { @link DynaBean}s. However, it is the application's
* responsibility to persist any such changes back to the database,
* if it so desires.</p>
*/
public List getRows() {

return (this.rows);

}

// ------------------------------------------------------ Protected Methods

/**
* <p>Copy the column values for each row in the specified
* <code>ResultSet</code> into a newly created { @link DynaBean}, and add
* this bean to the list of { @link DynaBean}s that will later by
* returned by a call to <code>getRows()</code>.</p>
*
* @param resultSet The <code>ResultSet</code> whose data is to be
* copied
*
* @exception SQLException if an error is encountered copying the data
*/
protected void copy(ResultSet resultSet) throws SQLException {
int abs = 0;
int rowsCount = 0;
int currentPageRows = 0;
resultSet.last();
rowsCount = resultSet.getRow();
if (pageSize != -1) {
int totalPages = (int) Math.ceil(((double) rowsCount) / pageSize);
if (page > totalPages)
page = totalPages;
if (page < 1)
page = 1;
abs = (page - 1) * pageSize;

//currentPageRows=(page==totalPages?rowsCount-pageSize*(totalPages-1):pageSize);
} else
pageSize = rowsCount;
if (abs == 0)
resultSet.beforeFirst();
else
resultSet.absolute(abs);
//int
while (resultSet.next() && ++currentPageRows <= pageSize) {
DynaBean bean = new BasicDynaBean(this);
for (int i = 0; i < properties.length; i++) {
String name = properties[i].getName();
bean.set(name, resultSet.getObject(name));
}
rows.add(bean);
}

}

/**
* <p>Introspect the metadata associated with our result set, and populate
* the <code>properties</code> and <code>propertiesMap</code> instance
* variables.</p>
*
* @param resultSet The <code>resultSet</code> whose metadata is to
* be introspected
*
* @exception SQLException if an error is encountered processing the
* result set metadata
*/
protected void introspect(ResultSet resultSet) throws SQLException {

// Accumulate an ordered list of DynaProperties
ArrayList list = new ArrayList();
ResultSetMetaData metadata = resultSet.getMetaData();
int n = metadata.getColumnCount();
for (int i = 1; i <= n; i++) { // JDBC is one-relative!
DynaProperty dynaProperty = createDynaProperty(metadata, i);
if (dynaProperty != null) {
list.add(dynaProperty);
}
}

// Convert this list into the internal data structures we need
properties =
(DynaProperty[]) list.toArray(new DynaProperty[list.size()]);
for (int i = 0; i < properties.length; i++) {
propertiesMap.put(properties[i].getName(), properties[i]);
}

}

/**
* <p>Factory method to create a new DynaProperty for the given index
* into the result set metadata.</p>
*
* @param metadata is the result set metadata
* @param i is the column index in the metadata
* @return the newly created DynaProperty instance
*/
protected DynaProperty createDynaProperty(
ResultSetMetaData metadata,
int i)
throws SQLException {

String name = null;
if (lowerCase) {
name = metadata.getColumnName(i).toLowerCase();
} else {
name = metadata.getColumnName(i);
}
String className = null;
try {
className = metadata.getColumnClassName(i);
} catch (SQLException e) {
// this is a patch for HsqlDb to ignore exceptions
// thrown by its metadata implementation
}

// Default to Object type if no class name could be retrieved
// from the metadata
Class clazz = Object.class;
if (className != null) {
clazz = loadClass(className);
}
return new DynaProperty(name, clazz);

}

/**
* <p>Loads and returns the <code>Class</code> of the given name.
* By default, a load from the thread context class loader is attempted.
* If there is no such class loader, the class loader used to load this
* class will be utilized.</p>
*
* @exception SQLException if an exception was thrown trying to load
* the specified class
*/
protected Class loadClass(String className) throws SQLException {

try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = this.getClass().getClassLoader();
}
return (cl.loadClass(className));
} catch (Exception e) {
throw new SQLException(
"Cannot load column class '" + className + "': " + e);
}

}

}

转载于:https://my.oschina.net/u/1398304/blog/284760

相关文章:

C# 线程无法开启窗口的原因

在 C# 里面, 主窗口拥有主线程, 主线程产生子线程监控 Socket 埠, 子线程一收到数据流就会给主线程发送一个事件, 创建一个窗口. 现在的情况是子线程能够收到数据流, 主窗口能够收到子线程发送过来的事件, 能够创建一个窗口. 这个窗口有问题: 窗口状态像死掉程序的窗口一样, 反…

给力!斩获 GitHub 14000 Star,两周创办开源公司获数百万美元融资

作者 | 伍杏玲出品 | AI 科技大本营&#xff08;ID:rgznai100&#xff09;上世纪 90 年代初&#xff0c;21 岁大学生 Linus Torvalds 开源 Linux 操作系统&#xff0c;自此掀起全球开源浪潮。随后“中国 Linux 第一人”宫敏博士用手提肩背的方式将 20 盒磁带背回中国&#xff0…

root密码忘记怎么办

开机按e进入系统的紧急求援模式依次输入一下命令&#xff1a;mount -o remount&#xff0c;rw /sysrootchroot /sysrootecho "输入新密码" | passwd --stdin rootexitreboot转载于:https://blog.51cto.com/11552940/1971850

C#内容分页简单实现代码及祥解

//定义变量 int i,start,stop,t,stat,statt,pp,pagecount,pagesize; //变量初始值 stat0; statt0; start0;//开始查询的字符串位置&#xff0c;初始为0 stop0; pagesize2000;//定义每页至少显示字符串数 pagecount0; //获得当前的页数 paRequest.Params["page&qu…

灵活越障、花样空翻,腾讯轮腿式机器人亮相机器人顶会 ICRA

6月3日&#xff0c;腾讯轮腿式机器人Ollie&#xff08;奥利&#xff09;正式亮相。它像一个灵活的“轮滑小子”&#xff0c;能完成跳跃、360度空翻等高难度动作。 轮腿式机器人&#xff08;wheel-legged robot&#xff09;是近年来机器人研究的前沿领域。Ollie兼具轮式结构和腿…

android学习笔记之十服务(Service)

2019独角兽企业重金招聘Python工程师标准>>> Service拥有一个单独进程的模块. 1,继承自Service类,须实现public IBinder onBind(Intent intent) 2,通过startServie触发运行,stopService终止运行 生命周期: onCreate(如果是第一次运行) -> onStart -> onDest…

宝塔Linux常用命令

https://www.bt.cn/bbs/thread-1186-1-1.html 2017年3月8日发布全新架构的宝塔Linux 面板3.1Beta版&#xff0c;到现在的5.2.0正式版&#xff0c;历经100多天打磨&#xff0c;宝塔研发工程师做了大量优化适配&#xff0c;从最开始的只支持CentOS到现在支持CentOS、Ubuntu、Debi…

EMAIL发送系统(C#+基于SMTP认证) 2.0

这个是对于 EMAIL发送系统(C#基于SMTP认证) 1.0 的改版这个第一版是2002年11月写的&#xff0c;过了一年半了&#xff0c;才有人提意见&#xff0c;就修正了一下&#xff0c;因为后来做的项目一直用不上&#xff0c;也就没有注意到 前段时间有网友反馈了一些问题&#xff0c;这…

“奥利”来啦,腾讯Robotics X实验室跑出的“轮滑小子”

6月3日&#xff0c;腾讯Robotics X实验室的新成员——轮腿式机器人Ollie&#xff08;奥利&#xff09;正式亮相&#xff0c;它是机器狗Jamoca、Max和自平衡自行车之后&#xff0c;实验室又一创新成果。轮腿式机器人&#xff08;wheel-legged robot&#xff09;是近年来机器人研…

如何写一篇好的技术博客

在工作过程中&#xff0c;发现对很多东西都一知半解&#xff0c;不是很透澈&#xff0c;到头来很容易模糊&#xff0c;如果有一篇好的技术博客予以总结&#xff0c;一来即使忘记了&#xff0c;回国头来再看&#xff0c;仍然能 够从自己的思路中恢复&#xff1b;二来总结一下&am…

使用react心得

为什么80%的码农都做不了架构师&#xff1f;>>> 在使用react中,总会碰到这样那样的问题,如何解决这些问题,希望能够随着时间的积累,慢慢成熟! 在react中的renden函数里,不能使用setState这个方法,不然会死循环,原因:是因为setState会触发render,而render后又触发se…

将Byte数组转化为String

FCL得很多方法的返回值都是包含字符的Byte数组而不是返回一个String&#xff0c;这样的方法包含在如下的类中&#xff1a; System.Net.Sockets.Socket.Receive System.Net.Sockets.Socket.ReceiveFrom System.Net.Sockets.Socket.BeginReceive System.Net.Sockets.Socket.B…

[题解]UVA10054 The Necklace

链接&#xff1a;http://vjudge.net/problem/viewProblem.action?id18806 描述&#xff1a;给出一堆珠子&#xff0c;每个珠子有两种颜色&#xff0c;有一端颜色相同的珠子可以串在一起&#xff0c;问是否可以把所有珠子串在一起&#xff0c;并求其中一种方案。 思路&#xff…

程序员大厂不一定要进,算法必须要学!收藏89篇精选算法文章

为什么程序员都需要学算法&#xff1f;程序员对算法通常怀有复杂情感&#xff0c;算法很重要是共识&#xff0c;但是否每个程序员都必须学算法是主要的分歧点。很多人觉得像人工智能、数据搜索与挖掘这样高薪的工作才用得上算法&#xff0c;觉得算法深不可测。在面试中&#xf…

专有云到混合云,是云计算的下半场?

查获案件案值达数十亿&#xff0c;为国家挽回近十亿元税款&#xff0c;是海关情报系统在全国应用一年后交出的答卷。 海关情报系统是海关总署与阿里云专有云共同搭建海关大数据云平台后推出的首个应用。 专有云的使命&#xff1a;激发政企大脑潜能 十年前&#xff0c;自己动手D…

C# 2.0 的partial

partial 关键字的作用是将你的 class 分为多个部分&#xff0c;编译器会将多个部分拼到一起去。 public partial class SampleClass ...{ public void MethodA() ...{ } } public partial class SampleClass ...{ public void MethodB() ...{ } } 和 public class Sa…

findbugs:may expose internal representation by ret

2019独角兽企业重金招聘Python工程师标准>>> findbugs&#xff1a;1. *** getXXX() may expose internal representation by returning ***.getXXX 2. *** setXXX(DATE )may expose internal representation by storing an externally mutable object into setXXX *…

AI时代的幕后英雄:谁在生产高质量的AI训练数据?

在AI浪潮的推动下&#xff0c;软件正在朝着更「智能」的方向发展。2017年&#xff0c;特斯拉人工智能部门主管、李飞飞高徒Andrej Karpathy提出了「软件2.0」的概念。 什么是「软件2.0」&#xff1f;其实就是神经网络。 在「软件1.0」时代&#xff0c;程序员用Java、Python、…

Webpack 核心开发者 Sean Larkin 盛赞 Vue

dev.io 近日邀请了 Webpack 核心开发者 Sean Larkin 回答开发者提问&#xff0c;其中几个问提比较有意思&#xff0c;和掘金的小伙伴们分享一下。 先上点前菜&#xff1a; 有一个开发者问 Sean 如何成为一个热门项目的核心作者。Sean 没有一上来就说该做什么&#xff0c;而是先…

设计模式C#描述——单例与多例模式

设计模式C#描述——单例与多例模式 作为对象的创建模式&#xff0c;单例模式确保某一个类只有一个实例&#xff0c;而且自行实例化并向整个系统提供这个实例。这个类称为单例类。 单例模式有以下特点&#xff1a; 单例类只能有一个实例。 单例类必须自己创建自己的唯一实例。 单…

Nutch插件开发及发布流程

2019独角兽企业重金招聘Python工程师标准>>> 一&#xff0c;插件开发流程&#xff1a; 1&#xff0c;Nutch开发客户端环境搭建 2&#xff0c;plugin的源代码则保存在/src/java/org/apache/nutch/parse/self/ 类实现实例&#xff1a; public class CustomizedIndexin…

网红 AI 高仿坎爷发布说唱情歌,歌迷:堪比真人原声

来源 | Hyper超神经头图 | 下载于视觉中国近日&#xff0c;一个基于 Tacotron2 和 Transformer 实现文字转声音的 AI 应用——Uberduck.AI 破圈了&#xff0c;不少 TikTok 、YouTube 网红博主都在推荐这一神器。YouTube 的网红音乐艺术创意机构 Herr Fuchs 发布了一首新歌&…

设计模式C#描述——抽象工厂模式

设计模式C#描述——抽象工厂模式 阅读此文应先阅读简单工厂模式与工厂方法模式 抽象工厂模式是对象的创建模式&#xff0c;它是工厂方法模式的进一步推广。 假设一个子系统需要一些产品对象&#xff0c;而这些产品又属于一个以上的产品等级结构。那么为了将消费这些产品对象的责…

怎样才能学好Vue,听听尤雨溪怎么说?

如果你想问前端最值得学习的框架是什么&#xff0c;我一定会毫不犹豫地告诉你是Vue。无论你是技术小白还是前端工程师&#xff0c;Vue的重要性自不必多说。从首个Commit的提交到破茧重生的Vue3、Vite2&#xff0c;Vue凭借轻量级、简单易学等优势&#xff0c;不仅荣登GitHub Rep…

如何彻底卸载mysql(xp)

如何彻底卸载mysql 完整的卸载MySQL 5.x 的方法&#xff1a; 1、控制面板里的增加删除程序内进行删除 2、删除MySQL的安装文件夹C:\Program Files\MySQL&#xff0c;如果备份好&#xff0c;可以直接将文件夹全部删除 3、开始->运行-> regedit 看看注册表里这几个地方删…

(一)JNDI基础

一、简介 在Tomcat 4.1.27之后&#xff0c;在服务器上就直接增加了数据源的配置选项&#xff0c;直接在服务器上配置好数据源连接池即可。在J2EE服务器上保存着一个数据库的多个连接。每一个连接通过DataSource可以找到。DataSource被绑定在了JNDI树上&#xff08;为每一个Data…

C# Idioms: Enum还是Enum Class(枚举类)

原文排版格式&#xff1a;http://www.marshine.com) reversion:2004/5/28 修改说明:感谢Ninputer提到的CLS兼容问题&#xff0c;同时修改了原来版本没有提及的Equals改写&#xff0c;以及修改""重载的不完善代码&#xff0c;和增加enum struct内容 reversion:2004/6…

构建第三代人工智能核心能力,清华、阿里、RealAI等联合发布最新AI安全评估平台

科技是发展的利器&#xff0c;也可能成为风险的源头。近日&#xff0c;张钹院士在智源大会上表示&#xff0c;AI的发展带来了科技是发展的利器&#xff0c;也可能成为风险的源头。近日&#xff0c;张钹院士在智源大会上表示&#xff0c;AI的发展带来了新的风险和安全隐患。 在…

Java 事件响应

按钮按钮(JButton)在界面设计中用于激发动作事件。按钮可显示文本&#xff0c;当按钮被激活时&#xff0c;能激发动作事件。JButton常用构造方法有&#xff1a;JButton()&#xff1a;创建一个没有标题的按钮对象&#xff1b;JButton(String s)&#xff1a;创建一个标题为s的按钮…

C# Idioms: Safely方法

(原文排版格式 http://www.marshine.com) 名称 Safely Method 意图 通过方法保证返回有效&#xff08;不为空引用&#xff0c;null或Nothing&#xff09;的对象或抛出异常&#xff0c;当存在多个调用者时简化调用者需要处理null返回值的代码。 动机 一个存放对象的集合或类似功…