问题描述
packagecom.jdbc;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.ResultSetMetaData;importjava.sql.SQLException;publicclassORMTest{publicstaticvoidmain(String[]args)throwsSQLException,InstantiationException,IllegalAccessException,IllegalArgumentException,InvocationTargetException{//TODOAuto-generatedmethodstubUsersuser=(Users)getObject("selectidasId,nameasName,passwordasPasswordfromuserwhereid=5",Users.class);System.out.println(user);}staticObjectgetObject(Stringsql,Classclazz)throwsSQLException,InstantiationException,IllegalAccessException,IllegalArgumentException,InvocationTargetException{Connectionconn=null;PreparedStatementps=null;ResultSetrs=null;try{conn=JdbcUtils.getConnection();ps=conn.prepareStatement(sql);rs=ps.executeQuery();ResultSetMetaDatarsmd=rs.getMetaData();intcount=rsmd.getColumnCount();String[]colNames=newString[count];for(inti=1;i<=count;i++){colNames[i-1]=rsmd.getColumnLabel(i);//ps.setObject(i,cloNames);}Objectobject=null;if(rs.next()){System.out.println(rs.getInt("id"));System.out.println(rs.getString("name"));object=clazz.newInstance();for(inti=0;i<colNames.length;i++){StringcolName=colNames[i];StringmethodName="set"+colName;Method[]ms=clazz.getMethods();//Method[]ms=user.getClass().getMethods();for(Methodm:ms){if(methodName.equals(m.getName())){//System.out.println(rs.getObject(colName));//System.out.println(user);m.invoke(object,rs.getObject(colName));break;}}//datas.put(cloNames[i],rs.getObject(cloNames[i]));}}returnobject;}finally{JdbcUtils.free(rs,ps,conn);}}}如上代码,错误提示类型不匹配Exceptioninthread"main"java.lang.IllegalArgumentException:argumenttypemismatchatsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(UnknownSource)atjava.lang.reflect.Method.invoke(UnknownSource)atcom.jdbc.ORMTest.getObject(ORMTest.java:68)atcom.jdbc.ORMTest.main(ORMTest.java:29)
解决方案
解决方案二:
m.invoke(object,rs.getObject(colName));补充一下,这一行提示错误
解决方案三:
User类里的set方法参数类型和rs.getObject(colName)不匹配吧
解决方案四:
oracle中的number中不是映射出integer而是BigDecimal