public static JSONArray convertToJSON(ResultSet resultSet) ???????????throws Exception { ???????JSONArray jsonArray = new JSONArray(); ???????while (resultSet.next()) { ???????????int total_rows = resultSet.getMetaData().getColumnCount(); ???????????JSONObject obj = new JSONObject(); ???????????for (int i = 0; i < total_rows; i++) { ???????????????obj.put(resultSet.getMetaData().getColumnLabel(i + 1) ???????????????????????.toLowerCase(), resultSet.getObject(i + 1)); ???????????????jsonArray.put(obj); ???????????} ???????} ???????return jsonArray; ???} ???public static String convertToXML(ResultSet resultSet) ???????????throws Exception { ???????StringBuilder xmlArray = new StringBuilder("<results>"); ???????while (resultSet.next()) { ???????????int total_rows = resultSet.getMetaData().getColumnCount(); ???????????xmlArray.append("<result "); ???????????for (int i = 0; i < total_rows; i++) { ???????????????xmlArray.append(" ").append(resultSet.getMetaData().getColumnLabel(i + 1) ???????????????????????.toLowerCase()).append("=‘").append(resultSet.getObject(i + 1)).append("‘"); ???????????} ???????????xmlArray.append(" />"); ???????} ???????xmlArray.append("</results>"); ???????return xmlArray.toString(); ???}
Convert ResultSet to JSON and XML
原文地址:https://www.cnblogs.com/yshyee/p/10302029.html