JAVA jdbc ResultSet 通过反射机制转换为实体类Bean

最近项目又有一部分用到jdbc了。。。都快忘光了。。。复习了一下,在这记录一下。肯定有有问题的地方,欢迎指正。

  1. 转实体类工具
/**
 * 公司名称: 
 *  项目名称: 
 * 版本号  : 1.0
 * 创建时间:2017/3/24 16:19
 * 创建人:leo
 **/
package com.shinow.slec.utils;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明:
 *ResultSet转实体类
 * @author leo
 * @version Version 1.0
 * @project SLEC
 * @date 2017/3/24.
 */

public class GetBeanFromResultUtil {
    private GetBeanFromResultUtil() {
    }

    public static  List getBeans(ResultSet resultSet, Class className) {
        List list = new ArrayList();
        Field fields[] = className.getDeclaredFields();
        try {
            while (resultSet.next()) {
                T instance = className.newInstance();
                for (Field field : fields) {
                    Object result = resultSet.getObject(field.getName());
                    boolean flag = field.isAccessible();
                    field.setAccessible(true);
                    field.set(instance, result);
                    field.setAccessible(flag);
                }
                list.add(instance);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static  T getBean(ResultSet resultSet, Class className) {
        T instance = null;
        try {
            instance = className.newInstance();
            Field fields[] = className.getDeclaredFields();
            for (Field field : fields) {
                Object result = resultSet.getObject(field.getName());
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                field.set(instance, result);
                field.setAccessible(flag);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return instance;
    }

}
  1. jdbc工具
package com.shinow.slec.utils;

import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil{
    private static Properties prop = null;
    private JDBCUtil() {
    }
    static{
        try{
            prop = new Properties();
            prop.load(new FileReader(JDBCUtils.class.getClassLoader().getResource("jdbc-police.properties").getPath()));
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取连接
     * @throws ClassNotFoundException 
     * @throws SQLException 
     */
    public static Connection getConn() throws ClassNotFoundException, SQLException{
        // 1.注册数据库驱动
        Class.forName(prop.getProperty("jdbc.driver"));
        // 2.获取连接
        return DriverManager.getConnection(prop.getProperty("jdbc.url"), prop.getProperty("jdbc.username"), prop.getProperty("jdbc.password"));

    }
    /**
     * 关闭连接
     */
    public static void close(ResultSet rs, Statement stat,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                rs = null;
            }
        }
        if(stat!=null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                stat = null;
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                conn = null;
            }
        }

    }
}

你可能感兴趣的:(java)