通过JAVA的第三方接口向数据库插入数据

package com.zsxt;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@SpringBootTest
public class dept_List {
    @Test
    public static void main(String[] args) {
        //指定的接口,包含url
        String apiUrl = "***********";
        //第三方接口需要体统令牌校验才能访问数据
        String authorization = "************";
        //本地数据库的地址、账号、密码
        String dbUrl = "*************";
        String dbUser = "*************";
        String dbPassword = "*************";

        writeDataToDatabase(apiUrl, authorization, dbUrl, dbUser, dbPassword);
    }

    public static void writeDataToDatabase(String apiUrl, String authorization, String dbUrl, String dbUser, String dbPassword) {
        try {
            // 发送 HTTP 请求获取接口内容,包含 Authorization 在请求头中
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Authorization", authorization);

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();


            // 解析 JSON 格式的接口内容,提取主要内容
            String responseData = response.toString();
            JSONObject jsonObject = new JSONObject(responseData);

            JSONArray mainContentArray = jsonObject.getJSONArray("data");
            // 假设 JSON 数据的主要内容位于 mainContent 字段中

            System.out.println(mainContentArray);
            System.out.println(mainContentArray.length());
            System.out.println("运行成功");



            // 连接数据库
            Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
            // 假设数据库表已经存在,这里不再创建表


            for (int i =0;i<mainContentArray.length();i++){
                // 将数据写入数据库
                String insertQuery = "INSERT INTO dept_list (deptName, deptId, parentId,deptCode) VALUES (?, ?, ?,?)";
                PreparedStatement preparedStatement = conn.prepareStatement(insertQuery);

                // 设置预编译语句的参数,假设从主要内容中获取
                preparedStatement.setString(1, mainContentArray.getJSONObject(i).getString("deptName"));
                preparedStatement.setString(2, mainContentArray.getJSONObject(i).getString("deptId"));
                preparedStatement.setString(3, mainContentArray.getJSONObject(i).getString("parentId"));
                preparedStatement.setString(4, mainContentArray.getJSONObject(i).getString("deptCode"));
                // ...

                // 执行插入操作
                preparedStatement.executeUpdate();
                // 关闭连接
                preparedStatement.close();

            }

            conn.close();

            System.out.println("Data has been successfully written to the database.");

        } catch (IOException | SQLException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

}
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值