1 PackageCom.xxyh.jdbc;2 Importjava.lang.reflect.InvocationTargetException;3 ImportJava.lang.reflect.Method;4 Importjava.sql.Connection;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 ImportJava.sql.ResultSetMetaData;8 Importjava.sql.SQLException;9 ImportCom.xxyh.jdbc.domain.User;Ten Public classOrmtest { One A Public Static voidMain (string[] args)throwsillegalaccessexception, IllegalArgumentException, InvocationTargetException, SQLException { -User user = GetUser ("SELECT ID as ID, name as name, birthday as birthday, money as money from user where id=1"); - System.out.println (user); the } - - StaticUser getUser (String sql)throwsSQLException, Illegalaccessexception, IllegalArgumentException, invocationtargetexception { -Connection conn =NULL; +PreparedStatement PS =NULL; -ResultSet rs =NULL; + Try { Aconn =jdbcutils.getconnection (); atPS =conn.preparestatement (SQL); -rs =ps.executequery (); - -ResultSetMetaData RSMD =Rs.getmetadata (); - intCount =Rsmd.getcolumncount (); -string[] Colnames =NewString[count]; in for(inti = 1; I <= count; i++) -COLNAMES[I-1] =Rsmd.getcolumnlabel (i); to +User User =NULL; - the if(Rs.next ()) { *user =NewUser (); $ for(inti = 0; i < colnames.length; i++) {Panax NotoginsengString colname =Colnames[i]; -String methodName = "Set" +colname; theMethod[] Methods =User.getclass (). GetMethods (); + for(Method method:methods) { A if(Methodname.equals (Method.getname ())) { the method.invoke (User, Rs.getobject (colname)); + } - } $ } $ } - returnuser; -}finally { the Jdbcutils.close (RS, PS, conn); - }Wuyi the } -}
"Run result": [1,zhangs,1985-01-01,360.0] Note: The output is shown as above because the ToString method of user is overridden:
@Override Public String toString () { return "[" + getId () + "," + getName () + "," + getbirthday () + "," + Getmoney () + "]";}
By using generics to improve the generality of the program, the GetObject method is independent of the specific class:
1 PackageCom.xxyh.jdbc;2 Importjava.lang.reflect.InvocationTargetException;3 ImportJava.lang.reflect.Method;4 Importjava.sql.Connection;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 ImportJava.sql.ResultSetMetaData;8 Importjava.sql.SQLException;9 ImportCom.xxyh.jdbc.domain.User;Ten Public classOrmtest { One A Public Static voidMain (string[] args)throwsillegalaccessexception, IllegalArgumentException, InvocationTargetException, SQLException, instantiationexception { -User user = (user) GetObject ("SELECT ID as ID, name as name, birthday as birthday, money as money from user where id=2", Us Er.class); - System.out.println (user); the } - - StaticObject getObject (String sql, class<?> clazz)throwsSQLException, Illegalaccessexception, IllegalArgumentException, InvocationTargetException, instantiationexception { -Connection conn =NULL; +PreparedStatement PS =NULL; -ResultSet rs =NULL; + Try { Aconn =jdbcutils.getconnection (); atPS =conn.preparestatement (SQL); -rs =ps.executequery (); - -ResultSetMetaData RSMD =Rs.getmetadata (); - intCount =Rsmd.getcolumncount (); -string[] Colnames =NewString[count]; in for(inti = 1; I <= count; i++) -COLNAMES[I-1] =Rsmd.getcolumnlabel (i); to +Object object =NULL; -Method[] Methods =clazz.getmethods (); the if(Rs.next ()) { *Object = Clazz.newinstance ();//Create an Object $ for(inti = 0; i < colnames.length; i++) {Panax NotoginsengString colname =Colnames[i]; -String methodName = "Set" +colname; the for(Method method:methods) { + if(Methodname.equals (Method.getname ())) { A Method.invoke (object, Rs.getobject (colname)); the } + } - } $ } $ returnobject; -}finally { - Jdbcutils.close (RS, PS, conn); the } - Wuyi } the}
"Run result": [2,lisi,1986-01-01,900.0]
JDBC Learning Note (16): Using Reflection Technology Award query results encapsulated as objects