当前位置:网站首页>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
边栏推荐
- Rust: a simple example of TCP server and client
- ArcGIS license error -15 solution
- Deep learning classic network analysis and target detection (I): r-cnn
- QTableWidget使用讲解
- .104History
- Visualization of residential house prices
- 【ACM】509. 斐波那契数(dp五部曲)
- The difference between deep copy and shallow copy
- Crawl lottery data
- Go的Gin框架学习
猜你喜欢
Visualization of residential house prices
GDAL + ogr learning
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Go的Gin框架学习
mysql自动启动设置用Systemctl start mysqld启动
How to install jsonpath package
Go语言JSON包使用
Fashion classification case based on keras
Implementation of image recognition code based on VGg convolutional neural network
Deep learning classic network analysis and target detection (I): r-cnn
随机推荐
Robocode tutorial 5 - enemy class
Dock installation redis
Implement a simple function to calculate the sum of all integers between M ~ n (m < n)
An example of linear regression based on tensorflow
.105Location
I/O多路复用及其相关详解
.104History
Crack sliding verification code
re正则表达式
GDAL + ogr learning
Mode of interprocess communication
C network related operations
C language input and output (printf and scanf functions, putchar and getchar functions)
【ACM】509. 斐波那契数(dp五部曲)
Stanford machine learning course summary
Operators in C language
I / O multiplexing and its related details
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
Flash operates on multiple databases