Json object from database in java

前端 未结 4 553
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 14:53

Can anyone help me how to create a JSON Object from the database?

This is what the JSON output should look like:

{“devicelist”:{
    “de         


        
4条回答
  •  死守一世寂寞
    2021-02-04 15:16

    package com.idal.cib;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class DatabaseConnector {
    
        static Connection conn1 = null;
    
        public static Connection getDbConnection(String driver, String url,
                String username, String password) {
            // TODO Auto-generated constructor stub
            try {
    
                Class.forName(driver);
    
                conn1 = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return conn1;
        }
    
    }
    
    
    package com.idal.cib;
    
    import java.io.FileWriter;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    
    public class DBJsonConverter {
    
        static ArrayList data = new ArrayList();
        static Connection conn = null;
        static PreparedStatement ps = null;
        static ResultSet rs = null;
        static String path = "";
        static String driver="";
        static String url="";
        static String username="";
        static String password="";
        static String query="";
    
        @SuppressWarnings({ "unchecked" })
        public static void dataLoad(String path) {
            JSONObject obj1 = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            conn = DatabaseConnector.getDbConnection(driver, url, username,
                    password);
            try {
                ps = conn.prepareStatement(query);
                rs = ps.executeQuery();
                ArrayList columnNames = new ArrayList();
                if (rs != null) {
                    ResultSetMetaData columns = rs.getMetaData();
                    int i = 0;
                    while (i < columns.getColumnCount()) {
                        i++;
                        columnNames.add(columns.getColumnName(i));
                    }
                    while (rs.next()) {
                        JSONObject obj = new JSONObject();
                        for (i = 0; i < columnNames.size(); i++) {
                            data.add(rs.getString(columnNames.get(i)));
                            {
                                for (int j = 0; j < data.size(); j++) {
                                    if (data.get(j) != null) {
                                        obj.put(columnNames.get(i), data.get(j));
                                    }else {
                                        obj.put(columnNames.get(i), "");
                                    }
                                }
                            }
                        }
    
                        jsonArray.add(obj);
                        obj1.put("header", jsonArray);
                        FileWriter file = new FileWriter(path);
                        file.write(obj1.toJSONString());
                        file.flush();
                        file.close();
                    }
                    ps.close();
                } else {
                    JSONObject obj2 = new JSONObject();
                    obj2.put(null, null);
                    jsonArray.add(obj2);
                    obj1.put("header", jsonArray);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                        rs.close();
                        ps.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @SuppressWarnings("static-access")
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            driver = "oracle.jdbc.driver.OracleDriver";
            url = "jdbc:oracle:thin:@10.11.34.134:1521:COREMUAT";
            username = "oasisusr";
            password = "p#g_ay0w";
            path = "D:\\VF_Polaris\\968670\\category_list1.json";
            query = "select * from temp_employee";
    
            DatabaseConnector dc = new DatabaseConnector();
            dc.getDbConnection(driver,url,username,password);
            DBJsonConverter formatter = new DBJsonConverter();
            formatter.dataLoad(path);
    
        }
    
    }
    

提交回复
热议问题