Query encapsulation-reflection and jdbc encapsulation reflection in jdbc

Source: Internet
Author: User

Query encapsulation-reflection and jdbc encapsulation reflection in jdbc

Query statements are similar when native jdbc is used. Therefore, reflection is used to encapsulate the query statements in jdbc.

Query a single statement
Query multiple statements

Package src. jdbcTest; import java. lang. reflect. field; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. resultSetMetaData; import java. SQL. SQLException; import java. util. arrayList; import java. util. list; import org. apache. log4j. logger;/***** @ author chencong * @ Time 12:37:55 on January 1, August 3, 2017 * @ TODO returns single and multi-row records through reflection */public cl Ass DBUtils {private static final String DRIVER_URL = "jdbc: sqlserver: // localhost: 1433; DataBaseName = mangerQQSys"; private static final String DRIVER_NAME = "com. microsoft. sqlserver. jdbc. SQLServerDriver "; private static final String DB_USER =" sa "; private static final String DB_PASSWORD =" 123456 "; private Connection connection = null; private PreparedStatement preparedStatement = null; private R EsultSet resultSet = null; public DBUtils () {}{ try {Class. forName (DRIVER_NAME); connection = DriverManager. getConnection (DRIVER_URL, DB_USER, DB_PASSWORD);} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (SQLException e) {e. printStackTrace () ;}} public Connection getConnection () {return connection;}/*** close database connection ** @ param Connection * @ param ptStatement * @ param resultS Et */public void closeAll (Connection connection, PreparedStatement ptStatement, ResultSet resultSet) {try {if (connection! = Null) {connection. close () ;}if (ptStatement! = Null) {ptStatement. close ();} if (resultSet! = Null) {ptStatement. close () ;}} catch (SQLException e) {e. printStackTrace () ;}}/*** close database connection ** @ param ptStatement * @ param resultSet */public void closeAll (PreparedStatement ptStatement, ResultSet resultSet) {try {if (ptStatement! = Null) {ptStatement. close ();} if (resultSet! = Null) {ptStatement. close () ;}} catch (SQLException e) {e. printStackTrace ();}} /*** the reflection mechanism returns a single record T ** @ param SQL * @ param params * @ param clazz * @ return * @ throws Exception */public <T> T selectSimpleResult (String SQL, list <Object> params, Class <T> clazz) throws Exception {if (connection = null) {return null;} if (SQL = null | clazz = null) {return null;} T resultObject = null; in T index = 1; preparedStatement = connection. prepareStatement (SQL); if (params! = Null &&! Params. isEmpty () {for (int I = 0; I <params. size (); I ++) {preparedStatement. setObject (index, params. get (I); index ++ ;}} resultSet = preparedStatement.exe cuteQuery (); ResultSetMetaData metaData = resultSet. getMetaData (); int couLength = metaData. getColumnCount (); while (resultSet. next () {// reflection to a generic object resultObject = clazz. newInstance (); for (int I = 0; I <couLength; I ++) {String colName = MetaData. getColumnName (I + 1); Object colValue = resultSet. getObject (colName); if (colValue = null) {colValue = "";} Field field = clazz. getDeclaredField (colName); field. setAccessible (true); // cancel the Java check mechanism to open the access permission field. set (resultObject, colValue) ;}return resultObject ;} /*** query multiple records ** @ param SQL * the SQL statement to be executed * @ param params * @ param clazz * @ return * @ throws Exception */public <T> list <T> selectMoreResult (String SQL, List <Object> params, Class <T> clazz) throws Exception {if (connection = null) {return null ;} if (SQL = null | clazz = null) {return null;} List <T> list = new ArrayList <T> (); int index = 1; preparedStatement = connection. prepareStatement (SQL); if (params! = Null &&! Params. isEmpty () {for (int I = 0; I <params. size (); I ++) {System. out. println (index + ":" + params. get (I); preparedStatement. setObject (index, params. get (I); index ++ ;}} resultSet = preparedStatement.exe cuteQuery (); ResultSetMetaData metaData = resultSet. getMetaData (); int colLength = metaData. getColumnCount (); while (resultSet. next () {// create object T resultObject = clazz through reflection. newInstance (); for (int I = 0; I <colLength; I ++) {String colName = metaData. getColumnName (I + 1); Object colValue = resultSet. getObject (colName); if (colValue = null) {colValue = "";} Field field = clazz. getDeclaredField (colName); field. setAccessible (true); field. set (resultObject, colValue);} list. add (resultObject) ;}return list ;}}
Cong's independent blog

Cong's independent blog, a post 95 who likes technology and interests in research.

  • Blog @ ccoder's blog
  • CSDN @ ccoder
  • Github @ ccoder
  • Email @ ccoderOrGmail @ ccoder
Copyright Disclaimer: This article is an original article by the blogger. It cannot be reproduced without the permission of the blogger. Please indicate the source for reprinting.

Related Article

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.