JDBC Learning Note (16): Using Reflection Technology Award query results encapsulated as objects

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.