JDBC 利用反射技术将查询结果封装为对象(简单ORM实现)

ORM(Object Relational Mapping)对象关系映射

 

public class ORMTest {

    public static void main(String[] args) throws SQLException,

            IllegalAccessException, InvocationTargetException, Exception {

        User user = (User) getObject(

                "select id as Id, name as Name, birthday as Birthday, money as Money  from user where id=1",

                User.class);

        System.out.println(user);

 

        Bean b = (Bean) getObject(

                "select id as Id, name as Name, birthday as Birthday, money as Money from user where id=1",

                Bean.class);

        System.out.println(b);

    }

 

    static List<Object> getObjects(String sql, Class clazz)

            throws SQLException, Exception, IllegalAccessException,

            InvocationTargetException {

        Connection conn = null;

        PreparedStatement ps = null;

        ResultSet rs = null;

        try {

            conn = JdbcUtils.getConnection();

            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();

            String[] colNames = getColNames(rs);

 

            List<Object> objects = new ArrayList<Object>();

            Method[] ms = clazz.getMethods();

            while (rs.next()) {

                Object object = clazz.newInstance();

                for (int i = 0; i < colNames.length; i++) {

                    String colName = colNames[i];

                    String methodName = "set" + colName;

                    // Object value = rs.getObject(colName);

                    // try {

                    // Method m = clazz

                    // .getMethod(methodName, value.getClass());

                    // if (m != null)

                    // m.invoke(object, value);

                    // } catch (NoSuchMethodException e) {

                    // e.printStackTrace();

                    // //

                    // }

                    for (Method m : ms) {

                        if (methodName.equals(m.getName())) {

                            m.invoke(object, rs.getObject(colName));

                            break;

                        }

                    }

                    objects.add(object);

                }

            }

            return objects;

        } finally {

            JdbcUtils.free(rs, ps, conn);

        }

    }

 

    private static String[] getColNames(ResultSet rs) throws SQLException {

        ResultSetMetaData rsmd = rs.getMetaData();

        int count = rsmd.getColumnCount();

        String[] colNames = new String[count];

        for (int i = 1; i <= count; i++) {

            colNames[i - 1] = rsmd.getColumnLabel(i);

        }

        return colNames;

    }

 

    static Object getObject(String sql, Class clazz) throws SQLException,

            Exception, IllegalAccessException, InvocationTargetException {

        Connection conn = null;

        PreparedStatement ps = null;

        ResultSet rs = null;

        try {

            conn = JdbcUtils.getConnection();

            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();

            String[] colNames = getColNames(rs);

 

            Object object = null;

            Method[] ms = clazz.getMethods();

            if (rs.next()) {

                object = clazz.newInstance();

                for (int i = 0; i < colNames.length; i++) {

                    String colName = colNames[i];

                    String methodName = "set" + colName;

                    // Object value = rs.getObject(colName);

                    // try {

                    // Method m = clazz

                    // .getMethod(methodName, value.getClass());

                    // if (m != null)

                    // m.invoke(object, value);

                    // } catch (NoSuchMethodException e) {

                    // e.printStackTrace();

                    // //

                    // }

                    for (Method m : ms) {

                        if (methodName.equals(m.getName())) {

                            m.invoke(object, rs.getObject(colName));

                            break;

                        }

                    }

                }

            }

            return object;

        } finally {

            JdbcUtils.free(rs, ps, conn);

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值