zoukankan      html  css  js  c++  java
  • java.sql.SQLException: Column 'class' not found.异常没有找到列

     1 /**处理当个对象的 ,rs中要么有一条,要么一条没有。
     2  * @param <T>
     3  */
     4 public class BeanHandler<T> implements ResultSetHandler<T> {
     5     private Class<T> type;
     6     public BeanHandler(Class<T> type){
     7         this.type = type;
     8     }
     9     /**将rs->T t
    10      */
    11     public T handle(ResultSet rs) throws SQLException {
    12         //1使用反射技术创建T类型的对象t
    13         //type ->User.class  
    14         //type.newInstance() “等价于”User user = new User();
    15         T t =null;
    16         try {
    17             t = type.newInstance();
    18             if(rs.next()){
    19                 //User.class ->BeanInfo info
    20                 BeanInfo info = Introspector.getBeanInfo(type);
    21                 //从info中获取所有属性对应的对象(属性类型,属性名称,setXxx和getXxx)
    22                 PropertyDescriptor[] pds = info.getPropertyDescriptors();
    23                 //遍历数组
    24                 for(int i =0;i<pds.length;i++){
    25                     //获得当前t对象的当前属性对应的setXxx(..)
    26                     Method mt = pds[i].getWriteMethod();
    27                     //获取当前属性的名称 比如:username
    28                     String pname = pds[i].getName();
    29                     //t.setXxx(rs.getString("属性名称"))
    30                     mt.invoke(t, rs.getObject(pname));
    31                 }
    32             }
    33             return t;
    34         } catch (Exception e) {
    35             e.printStackTrace();
    36         } 
    37         
    38         return t;
    39     }
    40 }

    测试,抛出了异常:
    java.sql.SQLException: Column 'class' not found.
    出错原因是:
    rs.getObject("class");
    数据库的表user表中不存在名称为class的列。
    我们在User类根本没有class,为何会有这一列的出现呢?
    原因是User没有指定父类时,某人是Object的子类,从Object类中继承了class属性,故此
    出现类class列不存在的问题。
    解决办法:

    1 try{
    2 mt.invoke(t, rs.getObject(pname));
    3 }catch (SQLException e) {
    4 continue;
    5 }
  • 相关阅读:
    Bacula Plugins
    getopt、getopt_long命令参数
    Notepad++ 快捷键
    make命令
    Linux目录结构
    rhel安装输入法
    libtool编译
    install和cp
    dlopen动态链接库操作
    结构体赋值
  • 原文地址:https://www.cnblogs.com/pxffly/p/7575368.html
Copyright © 2011-2022 走看看