java.sql.SQLException: Column 'class' not found.異常沒有找到列

來源:互聯網
上載者:User

標籤:type   tor   資料   繼承   ace   .sql   property   問題   name   

 

 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 t10      */11     public T handle(ResultSet rs) throws SQLException {12         //1使用反射技術建立T類型的對象t13         //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 info20                 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                     //擷取當前屬性的名稱 比如:username28                     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 }

 

java.sql.SQLException: Column 'class' not found.異常沒有找到列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.