当前位置:网站首页>According to the result set queried by SQL statement, it is encapsulated as JSON
According to the result set queried by SQL statement, it is encapsulated as JSON
2022-04-23 18:08:00 【dawnsun001】
according to SQL Statement to query the result set , Encapsulate it as json
for example :
1.select id,username,password,sex from user where id=11
{id:1,username:'jibs',password:'ssdfsdfsfwe',sex:' male '}
/**
* adopt sql Query results are encapsulated into json
* <br />
* <p>Example:</p>
* <code>String sql = "select id,username,password,sex from user where id=12";</code>
* @param sql Inquire about SQL
* @return {"id":"1","username":"jibs","password":"ssdfsdfsfwe","sex":" male "}
*/
Code :
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
array = new JSONArray();
// Get the number of columns
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
// Traverse ResultSet Each data in the
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// Traverse each column
for (int i = 1; i <= columnCount; i++) {
String columnName =metaData.getColumnLabel(i);
String value = rs.getString(columnName);
jsonObj.put(columnName, value);
}
array.put(jsonObj);
}
// adopt key Get each corresponding 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);
// Get data item
String value = resultObj.getString(columnName);
logger.info(value);
}
}
// test json The number of data
// int ii = array.length();
rs.close();
} catch (Exception e) {
logger.error(e.getMessage());
}
return array.toString();
}
版权声明
本文为[dawnsun001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544289296.html
边栏推荐
- Clion installation tutorial
- I / O multiplexing and its related details
- ROS package NMEA_ navsat_ Driver reads GPS and Beidou Positioning Information Notes
- Flash operates on multiple databases
- Docker 安装 MySQL
- C language array processing batch data
- powerdesigner各种字体设置;preview字体设置;sql字体设置
- Process management command
- 纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
- Notes on common basic usage of eigen Library
猜你喜欢
Implementation of k8s redis one master multi slave dynamic capacity expansion
Scikit learn sklearn 0.18 official document Chinese version
Jenkspy package installation
JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
.105Location
解决允许在postman中写入注释请求接口方法
【ACM】455. 分发饼干(1. 大饼干优先喂给大胃口;2. 遍历两个数组可以只用一个for循环(用下标索引--来遍历另一个数组))
C language loop structure program
How to install jsonpath package
随机推荐
Jenkspy package installation
MySQL 中的字符串函数
Multi thread crawling Marco Polo network supplier data
Pointers in rust: box, RC, cell, refcell
Classification of cifar100 data set based on convolutional neural network
What are the relationships and differences between threads and processes
Rust: the output information of println is displayed during the unit test
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
Process management command
Crawl lottery data
JD-FreeFuck 京东薅羊毛控制面板 后台命令执行漏洞
Batch export ArcGIS attribute table
Serialization scheme of serde - trust
From source code to executable file
Gst-launch-1.0 usage notes
Tensorflow tensor introduction
MATLAB小技巧(6)七种滤波方法比较
Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
Notes on common basic usage of eigen Library