当前位置:网站首页>根据SQL语句查询出的结果集,将其封装为json
根据SQL语句查询出的结果集,将其封装为json
2022-04-23 05:45:00 【dawnsun001】
根据SQL语句查询出的结果集,将其封装为json
例如:
1.select id,username,password,sex from user where id=11
{id:1,username:'jibs',password:'ssdfsdfsfwe',sex:'男'}
/**
* 通过sql查询结果封装成json
* <br />
* <p>Example:</p>
* <code>String sql = "select id,username,password,sex from user where id=12";</code>
* @param sql 查询SQL
* @return {"id":"1","username":"jibs","password":"ssdfsdfsfwe","sex":"男"}
*/
代码:
package com.dawnsun.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class TestSql2Json {
public static void main(String args[]) throws JSONException{
Connection conn = null;
Statement stat = null;
String url = "jdbc:mysql://localhost:3306/databasename";
String sql ="select * from student";
ResultSet rs = null;
JSONArray array = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,"username","password");
stat = conn.createStatement();
rs = stat.executeQuery(sql);
// json数组
array = new JSONArray();
// 获取列数
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// 遍历ResultSet中的每条数据
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// 遍历每一列
for (int i = 1; i <= columnCount; i++) {
String columnName =metaData.getColumnLabel(i);
String value = rs.getString(columnName);
jsonObj.put(columnName, value);
}
array.put(jsonObj);
}
//通过key获取每个对应value
for(int j=0;j<array.length();j++){
for(int i=1;i<columnCount;i++){
String columnName =metaData.getColumnLabel(i);
JSONObject resultObj = array.optJSONObject(j);
//获取数据项
String value = resultObj.getString(columnName);
logger.info(value);
}
}
//测试json数据的条数
// int ii = array.length();
rs.close();
} catch (Exception e) {
logger.error(e.getMessage());
}
return array.toString();
}
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dawnsun2013/article/details/19625619
边栏推荐
猜你喜欢
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
A general U-shaped transformer for image restoration
SQL -- data definition
Installation and usage skills of idea
A sharp tool to improve work efficiency
Motor and drive (Qi Jinqing Edition)
[leetcode 401] binary Watch
List segmentation best practices
線性代數第二章-矩陣及其運算
Storing inherited knowledge in cloud computing
随机推荐
What is the difference between the basic feasible solution and the basic feasible solution in linear programming?
The problem that the page will refresh automatically after clicking the submit button on the form is solved
MySQL best practices for creating tables
Usage scenario of copyonwritearraylist
SQL -- data definition
Use of multithreaded executors
St table template
Substring Inversion (Easy Version)
PHP processing JSON_ Decode() parses JSON stringify
Best practices for MySQL storage time
POI and easyexcel exercises
Formation à la programmation
P1586 solution to tetragonal theorem
[leetcode 459] duplicate substring
自动控制原理知识点整合归纳(韩敏版)
線性代數第一章-行列式
scikit-learn sklearn 0.18 官方文档中文版
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
Animation - Introduction to keyframes
Cf6d lizards and fundamentals 2 problem solving