纯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());
	}

 

 

上一篇:jdbc---查询语句的使用(executeQuery)


下一篇:★【LeetCode刷题记录】 - 最大子序和