当前位置:网站首页>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
边栏推荐
- C#字节数组(byte[])和字符串相互转换
- SSD硬盘SATA接口和M.2接口区别(详细)总结
- GDAL + ogr learning
- Selenium + webdriver + chrome realize Baidu to search for pictures
- Auto.js 自定义对话框
- Yolov4 pruning [with code]
- Gst-launch-1.0 usage notes
- Jenkspy package installation
- 解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
- 【ACM】376. 摆动序列
猜你喜欢
YOLOv4剪枝【附代码】
.105Location
Dock installation redis
powerdesigner各种字体设置;preview字体设置;sql字体设置
C language loop structure program
re正则表达式
positioner
MySQL_ 01_ Simple data retrieval
Closure type of rust (difference between FN, fnmut and fnone)
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
随机推荐
Crack sliding verification code
Robocode Tutorial 4 - robocode's game physics
MySQL_01_简单数据检索
Fashion classification case based on keras
NVIDIA Jetson: GStreamer and openmax (GST OMX) plug-ins
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
Gst-launch-1.0 usage notes
Svn simple operation command
Format problems encountered in word typesetting
【ACM】376. 摆动序列
Crawl the product data of cicada mother data platform
QTableWidget使用讲解
Crawl the product data of Xiaomi Youpin app
Jenkspy package installation
A few lines of code teach you to crawl lol skin pictures
Rust: how to implement a thread pool?
Box pointer of rust
ArcGIS table to excel exceeds the upper limit, conversion failed
PowerDesigner various font settings; Preview font setting; SQL font settings
re正則錶達式