java jdbc ResultSet results are assigned to Java objects through java reflection

In the case of unconformity framework, when using jdbc to read data from the database, we have to get and set one by one, which is not only tedious but also not concise. So we write a tool class using java reflection mechanism, so it is not so troublesome to use jdbc to get data from the database.

Because in many cases there is more than one piece of data, a collection of object classes is returned.

What should be noted: here, the database field naming format is user ﹣ name underline format, while the java type naming format is hump naming format.

The specific code is as follows:

 

package com.xc.sap.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import cnblogs.com.ooo0.oracle.OperateOracle;

public class Test {

    public static void main(String[] args) {
        try {
            // oracle Database connection
            OperateOracle oo = new OperateOracle();//https://www.cnblogs.com/ooo0/p/10225374.html
            Connection connection = oo.getConnection();

            String mainSql = "select * from users";
            PreparedStatement pstm = connection.prepareStatement(mainSql);
            ResultSet rs = pstm.executeQuery();

            ArrayList<Users> putResult = ResultSetPropertiesSimplifyHelps.putResult(rs, Users.class);

            for (int i = 0; i < putResult.size(); i++) {
                System.out.println(putResult.get(i).toString());
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

 

package com.xc.sap.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

/**
 * java jdbc ResultSet The result is assigned to a Java object through java reflection
 * 
 * @author xc
 */
public class ResultSetPropertiesSimplifyHelps {

    /**
     * Put the result of ResultSet into java object
     * 
     * @param <T>
     * @param rs
     *            ResultSet
     * @param obj
     *            java Class of class
     * @return
     */
    public static <T> ArrayList<T> putResult(ResultSet rs, Class<T> obj) {
        try {
            ArrayList<T> arrayList = new ArrayList<T>();
            ResultSetMetaData metaData = rs.getMetaData();
            /**
             * Get total columns
             */
            int count = metaData.getColumnCount();
            while (rs.next()) {
                /**
                 * Create an object instance
                 */
                T newInstance = obj.newInstance();
                for (int i = 1; i <= count; i++) {
                    /**
                     * Assign a value to an attribute of an object
                     */
                    String name = metaData.getColumnName(i).toLowerCase();
                    name = toJavaField(name);// Change column name format to java Naming format
                    String substring = name.substring(0, 1);// title case
                    String replace = name.replaceFirst(substring, substring.toUpperCase());
                    Class<?> type = null;
                    try {
                        type = obj.getDeclaredField(name).getType();// Get field type
                    } catch (NoSuchFieldException e) { // Class When the field is not defined by the object,skip
                        continue;
                    }

                    Method method = obj.getMethod("set" + replace, type);
                    /**
                     * Determine the type of data read
                     */
                    if (type.isAssignableFrom(String.class)) {
                        method.invoke(newInstance, rs.getString(i));
                    } else if (type.isAssignableFrom(byte.class) || type.isAssignableFrom(Byte.class)) {
                        method.invoke(newInstance, rs.getByte(i));// byte The data type is an 8-bit signed integer represented by a binary complement
                    } else if (type.isAssignableFrom(short.class) || type.isAssignableFrom(Short.class)) {
                        method.invoke(newInstance, rs.getShort(i));// short The data type is a 16 bit signed integer represented by a binary complement
                    } else if (type.isAssignableFrom(int.class) || type.isAssignableFrom(Integer.class)) {
                        method.invoke(newInstance, rs.getInt(i));// int The data type is a 32-bit signed integer represented by a binary complement
                    } else if (type.isAssignableFrom(long.class) || type.isAssignableFrom(Long.class)) {
                        method.invoke(newInstance, rs.getLong(i));// long The data type is a 64 bit signed integer represented by a binary complement
                    } else if (type.isAssignableFrom(float.class) || type.isAssignableFrom(Float.class)) {
                        method.invoke(newInstance, rs.getFloat(i));// float Data type is single precision, 32-bit, compliant IEEE 754 Standard floating point number
                    } else if (type.isAssignableFrom(double.class) || type.isAssignableFrom(Double.class)) {
                        method.invoke(newInstance, rs.getDouble(i));// double Data type is double, 64 bit, compliant IEEE 754 Standard floating point number
                    } else if (type.isAssignableFrom(BigDecimal.class)) {
                        method.invoke(newInstance, rs.getBigDecimal(i));
                    } else if (type.isAssignableFrom(boolean.class) || type.isAssignableFrom(Boolean.class)) {
                        method.invoke(newInstance, rs.getBoolean(i));// boolean Data type represents one bit of information
                    } else if (type.isAssignableFrom(Date.class)) {
                        method.invoke(newInstance, rs.getDate(i));
                    }
                }
                arrayList.add(newInstance);
            }
            return arrayList;

        } catch (InstantiationException | IllegalAccessException | SQLException | SecurityException | NoSuchMethodException | IllegalArgumentException
                | InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    /**
     * Database naming format to java Naming format
     * 
     * @param str
     *            Database field name
     * @return java Field name
     */
    public static String toJavaField(String str) {

        String[] split = str.split("_");
        StringBuilder builder = new StringBuilder();
        builder.append(split[0]);// Concatenate first character

        // If the array has more than one word
        if (split.length > 1) {
            for (int i = 1; i < split.length; i++) {
                // Remove underscores and capitalize initial
                String string = split[i];
                String substring = string.substring(0, 1);
                split[i] = string.replaceFirst(substring, substring.toUpperCase());
                builder.append(split[i]);
            }
        }

        return builder.toString();
    }

}

 

Reference article:

https://blog.csdn.net/a975261294/article/details/70049963

Optimization content:

1.NoSuchFieldException exception handling. If the Class object does not define this field, skip

2. Improved the value of most basic data types. http://www.runoob.com/java/java-basic-datatypes.html

Keywords: Java SQL Database JDBC

Added by jdpatrick on Fri, 24 Apr 2020 19:05:46 +0300