原生的JDBC获取URL中的数据,并存入到数据库

package hello;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
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;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class Jdbc {
    private static final String DRIVER_NAME = "com.mysql.jdbc.Driver";//mysql驱动包名
    private static final String URL = "jdbc:mysql://localhost:3306/root?serverTimezone=UTC";//数据库连接地址
    private static final String USER_NAME = "root";//用户名
    private static final String PASSWORD = "root";//密码

    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName(DRIVER_NAME);
        String url = URL;
        String user = USER_NAME;
        String password = PASSWORD;

        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

    public static void main(String[] args) {
        while (true) {
            List<Map<String, String>> list = httpURLConectionGET();
            if (list != null && list.size() > 0) {
                for (Map<String, String> map : list) {
                    if (map.get("price").isEmpty()) {
                        continue;
                    }
                    Connection connection = null;
                    int id = 0;
                    try {
                        PreparedStatement ps = null;
                        //获取数据库连接
                        connection = getConnection();
                        String sql = "INSERT INTO lemon (PRICE,TIME) VALUES(?,?)";
                        try {
                            ps = connection.prepareStatement(sql);
                            //获取 price 的值
                            ps.setDouble(1, Double.parseDouble(map.get("price")));
                            //获取时间戳的字符串,并转换为 Date
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            String timeText = sdf.format(map.get("time"));

                            //设置要读取的时间字符串格式
                            Date date = sdf.parse(timeText);
                            ps.setObject(2, date);
                            //执行sql语句
                            ps.executeUpdate();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        } finally {
                            connection.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (connection != null) {
                            try {
                                connection.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }

    
    public static final String GET_URL = "https://*******.com/*/*";

    /**
     * 接口调用 GET
     */
    public static List httpURLConectionGET() {
        try {
            URL url = new URL(GET_URL); // 把字符串转换为URL请求地址
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }

            br.close();// 关闭流
            connection.disconnect();// 断开连接
            ObjectMapper mapper = new ObjectMapper();
            Map readValue = mapper.readValue(sb.toString(), Map.class);
            List<Map<String, String>> list = (List<Map<String, String>>) readValue.get("data");

            return list;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败!");
            return null;
        }
    }
}

你可能感兴趣的:(面试题,mysql,java)