Beanutils 的 ResultSetDynaSet 中的错误?
Bug in Beanutils' ResultSetDynaSet?
我最近发现,JDBC有两种命名列的方式,即"by name"和"by label",而显然"by label"方式是默认的。
实际上,列名概念似乎让我感到困惑。可能不仅对我而言,对 Beanutils autors 来说也是如此,因为他们默认按名称访问字段(导致无法访问任何具有列别名的查询)。同时they have property useColumnLabel
,因为是内省后设置的,所以没有效果
这里是示例代码
import org.apache.commons.beanutils.*;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
public class ResultSetIteratorAttempt {
public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/sample","java","");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Id as NM1, Name as NM2 FROM DayOfWeek;");
ArrayList results = new ArrayList(); // To hold copied list
ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);
rsdc.setUseColumnLabel(true);
DynaProperty[] properties = rsdc.getDynaProperties();
String propertyName = "";
for(int i=0; i<properties.length; ++i) {
propertyName = properties[i].getName();
System.out.println(String.format("Property #%d is %s", i, propertyName));
}
Iterator rows = rsdc.iterator();
DynaBean row;
while (rows.hasNext()) {
row = (DynaBean) rows.next();
System.out.println(String.format("Row is %s", row.get(propertyName)));
}
}
}
我说得对吗?
UDPATE
mysql> select * from dayofweek;
+----+-----------+
| Id | Name |
+----+-----------+
| 5 | Friday |
| 1 | Monday |
| 6 | Saturday |
| 7 | Sunday |
| 4 | Thursday |
| 2 | Tuesday |
| 3 | Wednesday |
+----+-----------+
7 rows in set (0.00 sec)
错误已知https://issues.apache.org/jira/browse/BEANUTILS-383
解决方法是使用替代构造函数签名,它接受三个参数。
我最近发现,JDBC有两种命名列的方式,即"by name"和"by label",而显然"by label"方式是默认的。
实际上,列名概念似乎让我感到困惑。可能不仅对我而言,对 Beanutils autors 来说也是如此,因为他们默认按名称访问字段(导致无法访问任何具有列别名的查询)。同时they have property useColumnLabel
,因为是内省后设置的,所以没有效果
这里是示例代码
import org.apache.commons.beanutils.*;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
public class ResultSetIteratorAttempt {
public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/sample","java","");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT Id as NM1, Name as NM2 FROM DayOfWeek;");
ArrayList results = new ArrayList(); // To hold copied list
ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);
rsdc.setUseColumnLabel(true);
DynaProperty[] properties = rsdc.getDynaProperties();
String propertyName = "";
for(int i=0; i<properties.length; ++i) {
propertyName = properties[i].getName();
System.out.println(String.format("Property #%d is %s", i, propertyName));
}
Iterator rows = rsdc.iterator();
DynaBean row;
while (rows.hasNext()) {
row = (DynaBean) rows.next();
System.out.println(String.format("Row is %s", row.get(propertyName)));
}
}
}
我说得对吗?
UDPATE
mysql> select * from dayofweek;
+----+-----------+
| Id | Name |
+----+-----------+
| 5 | Friday |
| 1 | Monday |
| 6 | Saturday |
| 7 | Sunday |
| 4 | Thursday |
| 2 | Tuesday |
| 3 | Wednesday |
+----+-----------+
7 rows in set (0.00 sec)
错误已知https://issues.apache.org/jira/browse/BEANUTILS-383
解决方法是使用替代构造函数签名,它接受三个参数。