今天学习时候碰见特别费解的一个事情刚开始我还打算直接调用bean的赋值方法,结果被秒杀Method setMethod = User.class.getMethod(methodName,new Class[]{Object.class});
setMethod.invoke(user,rs.getObject(colName));这里是为什么呢?上面行参数可以是任何类型对象,调用时候就直接从数据库里面拿出来object也不判断了,各个字段类型肯定会又不一样的,结果出错!好吧,我承认我想简单了,我把JAVA的自动判断类型想得过于智能了。
但是看下面的教程代码,我也很崩溃,因为确实是method.invoke(t, rs.getObject(colName))我执行不过去,但是视频上面过去了,我这边报错很明显argument type mismatch,秒看懂:参数类型不匹配。
求理解,是代码有误,还是JDK原因,学习开始使用的是JDK7U15,视频的是传智的专门讲JDBC的视频mappingUser方法大家能看出来Bean和数据库的各个字段类型,在这里没作用。public class ORMTest
{
public static void main(String[] args) throws Exception
{
User users = getT("select id as Id,name as Name,birthday as Birthday,money as Money from user where id=1",User.class); System.out.println(users);
} static <T>T getT(String sql,Class<T> clazz) throws Exception
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null; try
{
conn = JdbcUtils.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
String[] colNames = new String[count];
for (int i = 1; i <= count; i++)
{
colNames[i - 1] = rsmd.getColumnLabel(i);
} T t = null;
Method[] methods = clazz.getMethods();
if (rs.next())
{
t = clazz.newInstance();
for (int i = 0; i < colNames.length; i++)
{
String colName = colNames[i]; // 数据库里面的列名,这里可能是SQL的别名 String methodName = "set" + colName; // 方法名 for (Method method : methods)
{
if (methodName.equals(method.getName()))
{
method.invoke(t, rs.getObject(colName));
}
} // Method setMethod = User.class.getMethod(methodName,new Class[]{Object.class});
// setMethod.invoke(user,rs.getObject(colName));
} } return t;
}
finally
{
JdbcUtils.free(rs, ps, conn);
}
}

// private User mappingUser(ResultSet rs) throws SQLException
// {
// User user = new User();
//
// user.setId(rs.getInt("id"));
// user.setName(rs.getString("name"));
// user.setBirthday(rs.getDate("birthday")); // 子类赋值给父类,可以不转换
// user.setMoney(rs.getFloat("money"));
//
// return user;
// }

}
JDBCreflect传智

解决方案 »

  1.   

    你试下在User users = getT("select id as Id,name as Name,birthday as Birthday,money as Money from user where id=1",User.class);的User.class外加个大括号。
      

  2.   


    不行,而且真心没必要因为方法本身定义时候 static <T>T getT(String sql,Class<T> clazz)就是传进去一个Class类,泛型只是为了方便以后用,表示穿进去的哪个就是输出的哪个
      

  3.   

    多简单个事,你以为getObject(i)得出来的就一定是Object类型?getObject(i).getClass()一个一个打出来看,该是什么就是什么。至于报错,本机上的错用断点直接看,人脑又不是机器,看代码就能找出来错。类型不匹配就一个一个看类型,是不是null,是不是数据库的特殊类型。
      

  4.   

    你那setMethod获取的不对  根本找不到setXxx(Object obj)这样的方法应该用PropertyDescriptor的getWriteMethod  好像是这么写的
      

  5.   


    谢谢回答,我问的是为什么视频里面可以跑起来这个代码,我却运行部起来import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;import com.nyohh.jdbc.domain.User;
    import com.nyohh.jdbc.utils.JdbcUtils;
    /**
     * 
     * 2008-12-7
     * 
     * @author <a href="mailto:[email protected]">liyong</a>
     * 
     */
    public class ORMTest2 { /**
     * @param args
     * @throws Exception
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException,
    IllegalAccessException, InvocationTargetException, Exception {
    User user = (User) getObject(
    "select id as Id, name as Name, birthday as Birthday, money as Money  from user where id=1",
    User.class);
    System.out.println(user);
    } static List<Object> getObjects(String sql, Class clazz)
    throws SQLException, Exception, IllegalAccessException,
    InvocationTargetException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
    conn = JdbcUtils.getConnection();
    ps = conn.prepareStatement(sql);
    rs = ps.executeQuery();
    String[] colNames = getColNames(rs); List<Object> objects = new ArrayList<Object>();
    Method[] ms = clazz.getMethods();
    while (rs.next()) {
    Object object = clazz.newInstance();
    for (int i = 0; i < colNames.length; i++) {
    String colName = colNames[i];
    String methodName = "set" + colName;
    // Object value = rs.getObject(colName);
    // try {
    // Method m = clazz
    // .getMethod(methodName, value.getClass());
    // if (m != null)
    // m.invoke(object, value);
    // } catch (NoSuchMethodException e) {
    // e.printStackTrace();
    // //
    // }
    for (Method m : ms) {
    if (methodName.equals(m.getName())) {
    m.invoke(object, rs.getObject(colName));
    break;
    }
    }
    objects.add(object);
    }
    }
    return objects;
    } finally {
    JdbcUtils.free(rs, ps, conn);
    }
    } private static String[] getColNames(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int count = rsmd.getColumnCount();
    String[] colNames = new String[count];
    for (int i = 1; i <= count; i++) {
    colNames[i - 1] = rsmd.getColumnLabel(i);
    }
    return colNames;
    } static Object getObject(String sql, Class clazz) throws SQLException,
    Exception, IllegalAccessException, InvocationTargetException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
    conn = JdbcUtils.getConnection();
    ps = conn.prepareStatement(sql);
    rs = ps.executeQuery();
    String[] colNames = getColNames(rs); Object object = null;
    Method[] ms = clazz.getMethods();
    if (rs.next()) {
    object = clazz.newInstance();
    for (int i = 0; i < colNames.length; i++) {
    String colName = colNames[i];
    String methodName = "set" + colName;
    // Object value = rs.getObject(colName);
    // try {
    // Method m = clazz
    // .getMethod(methodName, value.getClass());
    // if (m != null)
    // m.invoke(object, value);
    // } catch (NoSuchMethodException e) {
    // e.printStackTrace();
    // //
    // }
    for (Method m : ms) {
    if (methodName.equals(m.getName())) {
    m.invoke(object, rs.getObject(colName));
    break;
    }
    }
    }
    }
    return object;
    } finally {
    JdbcUtils.free(rs, ps, conn);
    }
    }
    }
      

  6.   

    另外getObject一定是Object的
    Object java.sql.ResultSet.getObject(String columnLabel) throws SQLExceptionGets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.