微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

纯JDBC查询数据库并转换为对应实体类的List集合

1、查询及转换方式如下 

            private static Log log = LogFactory.getLog(Dirlog.class);            
            String dirver = JdbcConfig.getDriver();
            //从配置文件读取数据库连接信息
            String url = JdbcConfig.getConnectUrl(num);
            String username = JdbcConfig.getUserName(num);
            String password = JdbcConfig.getpassword(num);
            String table = JdbcConfig.getTable(num);
            Connection connection = null;
            PreparedStatement statement = null;
            ResultSet resultSet = null;
            try {
                Class.forName(dirver);
                connection = DriverManager.getConnection(url, username, password);
                if (connection != null) {
                    //获取前一天的开始时间和结束时间
                    String startTime = DateUtil.getDate(1) + " 00:00:00";
                    String endTime = DateUtil.getDate(1) + " 23:59:59";
                    String sql = "SELECT * FROM "+table+" WHERE create_time BETWEEN '" + startTime +"' AND '" + endTime+"'";
                    statement = connection.prepareStatement(sql);
                    resultSet = statement.executeQuery();
                    ResultSetMetaData MetaData = resultSet.getMetaData();
                    int columnNum = MetaData.getColumnCount();
                    List<Map<String, Object>> list = new ArrayList<>();
                    while (resultSet.next()) {
                        Map<String, Object> rowData = new HashMap<>();
                        for (int i = 1; i <= columnNum; i++) {
                            rowData.put(MetaData.getColumnName(i), resultSet.getobject(i));
                        }
                        list.add(rowData);
                    }
                    //将查询到的表数据转换为中间表数据集合
                    JSONArray jsonArray = JSONArray.fromObject(list);
                    List<Eslogobj> esLogs = JSONArray.toList(jsonArray, Eslogobj.class);
                    //将中间表集合转换为标准的Log数据集合
                    List<Log> loglist = new ArrayList<>();
                    for (Eslogobj esLog : esLogs) {
                        loglist.add(esLog.convertToLog());
                    }
                    log.info("同步:"+url+"日志数量:"+loglist.size());
                }
            } catch (Exception e) {
                log.error("查询日志失败,连接地址:"+url+";失败原因:"+e.getMessage());
                e.printstacktrace();
            }finally {
                try {
                    if (connection != null){
                        connection.close();
                    }
                    if (resultSet != null){
                        resultSet.close();
                    }
                    if (statement != null){
                        statement.close();
                    }
                } catch (sqlException e) {
                    e.printstacktrace();
                }

2.DateUtil相关方法

/**
	 * 获取指定数量的天前的系统日期串,格式为"yyyy-MM-dd"
	 * @param days 天数
	 * @return
	 */
	public static String getDate(int days) {
		GregorianCalendar calTmp = new GregorianCalendar();
		calTmp.add(GregorianCalendar.DATE, -1 * days);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(calTmp.getTime());
	}

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐