英文:
Generating json file in database instead of java
问题
以下是翻译好的部分:
我从数据库中使用resultset
读取一个table
,然后使用这些数据生成json文件。
我想在数据库中生成一个json
文件,并将该文件返回给java
,因为我的java代码将要使用POST API
。
代码大致如下:
String query = "select * from dynamicentitygtt";
PreparedStatement prepstatement = connection.prepareStatement(query);
ResultSet rs = prepstatement.executeQuery();
while (rs.next()) {
// 将数据加载到POJO中
}
// 调用方法生成json文件。
// 将文件数据注入API
我的需求是:
String query = "select * from dynamicentitygtt";
PreparedStatement prepstatement = connection.prepareStatement(query);
ResultSet rs = prepstatement.executeQuery();
// 在这里,我希望从数据库获取文件。
// 我的数据库应该返回json文件。我可以在数据库中创建存储过程。我还可以使用可调用方法。我还可以从表中创建json文件。
// 但是我不知道如何从数据库返回文件。
如果我理解有误,请原谅我。
英文:
I am reading one table
from database using resultset
. later I am using this data to generate json file.
I want to generate json
file in database and return that file to java
because my java code is going to consume POST API.
code is something like this
String query="select * from dynamicentitygtt";
PreparedStatement prepstatement=connection.prepareStatement(query);
ResultSet rs=prepstatement.executeQuery()
while rs.next()
{
//Load data to POJO
}
//call method to generate json file.
//Inject file data to API
what I want.
String query="select * from dynamicentitygtt";
PreparedStatement prepstatement=connection.prepareStatement(query);
ResultSet rs=prepstatement.executeQuery()
//here I want file from database.
My database should return json file. i can create procedure in database. i can also use callable. I can also create json file from table.
But i dont know how to return file from database.
forgive me if my understanding is incorrect.
答案1
得分: 0
因为你已经注意到你正在使用Oracle 18c,所以你可以访问JSON_OBJECT,JSON_OBJECTAGG,JSON_ARRAY和JSON_ARRAYAGG SQL 函数。通过使用这些函数,你应该能够仅通过SQL查询创建所需的JSON结构,而无需生成文件并将其传递回Java调用。需要注意的一点是,如果你正在生成大型JSON对象,你需要修改查询中任何JSON函数调用的RETURNING子句,否则你的JSON将无效。
以下是如何使用这些函数的一些示例:
查询#1
WITH
emp (employee_id,
first_name,
last_name,
department_number)
AS
(SELECT 1, 'John', 'Doe', 100 FROM DUAL
UNION ALL
SELECT 2, 'Jane', 'Smith', 100 FROM DUAL
UNION ALL
SELECT 3, 'Robert', 'Jones', 200 FROM DUAL
UNION ALL
SELECT 4, 'Alan', 'Carter', 300 FROM DUAL)
SELECT json_arrayagg (json_object (*))
FROM emp;
结果#1(美化后)
[
{
"EMPLOYEE_ID": 1,
"FIRST_NAME": "John",
"LAST_NAME": "Doe",
"DEPARTMENT_NUMBER": 100
},
{
"EMPLOYEE_ID": 2,
"FIRST_NAME": "Jane",
"LAST_NAME": "Smith",
"DEPARTMENT_NUMBER": 100
},
{
"EMPLOYEE_ID": 3,
"FIRST_NAME": "Robert",
"LAST_NAME": "Jones",
"DEPARTMENT_NUMBER": 200
},
{
"EMPLOYEE_ID": 4,
"FIRST_NAME": "Alan",
"LAST_NAME": "Carter",
"DEPARTMENT_NUMBER": 300
}
]
查询#2
WITH
emp (employee_id,
first_name,
last_name,
department_number)
AS
(SELECT 1, 'John', 'Doe', 100 FROM DUAL
UNION ALL
SELECT 2, 'Jane', 'Smith', 100 FROM DUAL
UNION ALL
SELECT 3, 'Robert', 'Jones', 200 FROM DUAL
UNION ALL
SELECT 4, 'Alan', 'Carter', 300 FROM DUAL)
SELECT json_arrayagg (
json_object (
'department_number' VALUE department_number,
'employees' VALUE
json_arrayagg (
json_object ('first_name' VALUE first_name, 'last_name' VALUE last_name))))
FROM emp
GROUP BY department_number;
结果#2(美化后)
[
{
"department_number": 100,
"employees": [
{ "first_name": "John", "last_name": "Doe" },
{ "first_name": "Jane", "last_name": "Smith" }
]
},
{
"department_number": 200,
"employees": [{ "first_name": "Robert", "last_name": "Jones" }]
},
{
"department_number": 300,
"employees": [{ "first_name": "Alan", "last_name": "Carter" }]
}
]
英文:
Since you have noted that you are on Oracle 18c, you have access to the JSON_OBJECT, JSON_OBJECTAGG, JSON_ARRAY, and JSON_ARRAYAGG SQL functions. Using these functions, you should be able to create the JSON structure you need just with a SQL query instead of needing to generate a file and pass it back to your Java call. One item to note is that if you are generating large JSON objects, you will want to modify the RETURNING clause in any of the JSON function calls in the query or your JSON will not be valid.
Below are some examples on how to use the functions:
Query #1
WITH
emp (employee_id,
first_name,
last_name,
department_number)
AS
(SELECT 1, 'John', 'Doe', 100 FROM DUAL
UNION ALL
SELECT 2, 'Jane', 'Smith', 100 FROM DUAL
UNION ALL
SELECT 3, 'Robert', 'Jones', 200 FROM DUAL
UNION ALL
SELECT 4, 'Alan', 'Carter', 300 FROM DUAL)
SELECT json_arrayagg (json_object (*))
FROM emp;
Result #1 (beautified)
[
{
"EMPLOYEE_ID": 1,
"FIRST_NAME": "John",
"LAST_NAME": "Doe",
"DEPARTMENT_NUMBER": 100
},
{
"EMPLOYEE_ID": 2,
"FIRST_NAME": "Jane",
"LAST_NAME": "Smith",
"DEPARTMENT_NUMBER": 100
},
{
"EMPLOYEE_ID": 3,
"FIRST_NAME": "Robert",
"LAST_NAME": "Jones",
"DEPARTMENT_NUMBER": 200
},
{
"EMPLOYEE_ID": 4,
"FIRST_NAME": "Alan",
"LAST_NAME": "Carter",
"DEPARTMENT_NUMBER": 300
}
]
Query #2
WITH
emp (employee_id,
first_name,
last_name,
department_number)
AS
(SELECT 1, 'John', 'Doe', 100 FROM DUAL
UNION ALL
SELECT 2, 'Jane', 'Smith', 100 FROM DUAL
UNION ALL
SELECT 3, 'Robert', 'Jones', 200 FROM DUAL
UNION ALL
SELECT 4, 'Alan', 'Carter', 300 FROM DUAL)
SELECT json_arrayagg (
json_object (
'department_number' VALUE department_number,
'employees' VALUE
json_arrayagg (
json_object ('first_name' VALUE first_name, 'last_name' VALUE last_name))))
FROM emp
GROUP BY department_number;
Result #2 (beautified)
[
{
"department_number": 100,
"employees": [
{ "first_name": "John", "last_name": "Doe" },
{ "first_name": "Jane", "last_name": "Smith" }
]
},
{
"department_number": 200,
"employees": [{ "first_name": "Robert", "last_name": "Jones" }]
},
{
"department_number": 300,
"employees": [{ "first_name": "Alan", "last_name": "Carter" }]
}
]
答案2
得分: -1
使用以下方法,您可以在Java中创建一个List
,其中包含多个Map<String, String>
。
其中,每个Map
的键是列名,值为该列的值。
public List<Map<String, String>> getList(ResultSet resultSet) throws SQLException {
List<Map<String, String>> mapList = new ArrayList<>();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnNumber = resultSetMetaData.getColumnCount();
while (resultSet.next()) {
Map<String, String> map = new HashMap<>();
int count = 1;
while (count <= columnNumber) {
String columnName = resultSetMetaData.getColumnName(count);
map.put(columnName, resultSet.getString(columnName));
count++;
}
mapList.add(map);
}
return mapList;
}
一旦您成功创建了Map
,您可以使用多个类库将其转换为JSON字符串或JSON对象,类似于这样
。
这会对您有所帮助。
英文:
In java using below method you can create List of Map<String,String>.
Where Map have key of columnName and value as value of that column.
public List<Map<String, String>> getList(ResultSet resultSet) throws SQLException {
List<Map<String, String>> mapList = new ArrayList<>();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnNumber = resultSetMetaData.getColumnCount();
while (resultSet.next()) {
Map<String, String> map = new HashMap<>();
int count = 1;
while (count <= columnNumber) {
String columnName = resultSetMetaData.getColumnName(count);
map.put(columnName, resultSet.getString(columnName));
count++;
}
mapList.add(map);
}
return mapList;
}
Once you are able to create Map, you can create any json string or json object from it using multiple library like this
This will help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论