zoukankan      html  css  js  c++  java
  • JDBC操作数据封装Java Bean

                Connection conn = DriverManager.getConnection(dbURL, properties);
                if (conn != null) {
                    Statement statement = conn.createStatement();
                    ResultSet resultSet = statement.executeQuery("  select * from student");
    
                    BeanInfo beanInfo = Introspector.getBeanInfo(Student.class, Object.class);
    
                    while (resultSet.next()) {
                        Student student = new Student();
                        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
                            String propertyName = propertyDescriptor.getName();//属性名
                            Class<?> propertyType = propertyDescriptor.getPropertyType();//属性类型
                            String columnName = getTableMapperColumnName(propertyName);
                            String resultMethodStr = getResultMethod(propertyType.toString());//结果集取数方法
                            Method resultMethod = ResultSet.class.getMethod(resultMethodStr, String.class);
                            Object columnValue = resultMethod.invoke(resultSet, columnName);//结果集的值
    
                            Method writeMethod = propertyDescriptor.getWriteMethod();//Setter
                            writeMethod.invoke(student, columnValue);
    
                        }
                        System.out.println(student.toString());
                    }
    
                }
    
        private static String getTableMapperColumnName(String propertyName) {
            if (propertyName.equals("firstName")) {
                return "first_name";
            } else if (propertyName.equals("lastName")) {
                return "last_name";
            } else {
                return propertyName;
            }
        }
    
        private static String getResultMethod(String propertyType) {
    
            //return "get"+propertyType.substring(0,1).toUpperCase()+propertyType.substring(1,propertyType.length());
            switch (propertyType) {
                case "Integer":
                    return "getInteger";
                case "int":
                    return "getInt";
                case "String":
                    return "getString";
                case "Long":
                    return "getLong";
                default:
                    return "getString";
            }
    
        }
    
    
  • 相关阅读:
    vue 防抖 节流
    数组取最小数据长度,确定长度截取,看是否全等 ,全等通过不等提示,需要拆分
    数组去重取不重复的数据
    vue
    vue2.0 子组件获取父组件值 使用v-model可渲染不能更改
    使用mpvue 开发小程序 遇到的坑
    ztree 样式更改
    vue 跨域请求
    记录 vue2.0 再使用过程中遇到的问题
    bug
  • 原文地址:https://www.cnblogs.com/lanqie/p/14771760.html
Copyright © 2011-2022 走看看