1 /**when dealing with an object, there is either one in RS or one without it. 2 * @param<T>3 */4 Public classBeanhandler<t>ImplementsResultsethandler<t> {5 PrivateClass<t>type;6 PublicBeanhandler (class<t>type) {7 This. Type =type;8 }9 /**the rs->t TTen */ One PublicT handle (ResultSet RS)throwsSQLException { A //1 Creating an object of type T using reflection technology - //type->user.class - //type.newinstance () "is equivalent to" User user = new user (); theT t =NULL; - Try { -t =type.newinstance (); - if(Rs.next ()) { + //User.class->beaninfo Info -BeanInfo info =introspector.getbeaninfo (type); + //gets the object (attribute type, property name, Setxxx, and GetXXX) for all properties from info APropertydescriptor[] PDS =info.getpropertydescriptors (); at //iterating through an array - for(intI =0;i<pds.length;i++){ - //gets the current property of the current T object corresponding to the Setxxx (..) -Method MT =Pds[i].getwritemethod (); - //gets the name of the current property such as: Username -String pname =pds[i].getname (); in //t.setxxx (rs.getstring ("attribute name")) - Mt.invoke (t, Rs.getobject (pname)); to } + } - returnT; the}Catch(Exception e) { * e.printstacktrace (); $ } Panax Notoginseng - returnT; the } +}
Test, throws an exception:
Java.sql.SQLException:Column ' class ' not found.
The cause of the error is:
Rs.getobject ("class");
A column with the name class does not exist in the table user table of the database.
We have no class at all in the user class, why does this column appear?
The reason is that when user does not specify a parent class, someone is a subclass of object, inherits the class attribute from the object class, and therefore
There is a problem with the class column that does not exist.
Workaround:
1 Try {2mt.invoke (t, Rs.getobject (pname)); 3 }catch (SQLException e) {4continue; 5 }
Java.sql.SQLException:Column ' class ' not found. Exception not found column