每日总结

今日进行了hive数据库的增删改查

复制代码
package ceshi.spark;

import crud.pojo.LoadEst;

import java.sql.*;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

public class Dao {
    public List<LoadEst> selectAll(String d) {
        List<LoadEst> list = new ArrayList<>();
        Connection conn = null;
        try {
            conn = JDBC.getConnection();
            // 创建Statement对象
            Statement stmt = conn.createStatement();

            // 执行Hive查询
            String query = "select * from test1 where search_word LIKE '%" + d + "%'";
            ResultSet resultSet = stmt.executeQuery(query);

            // 处理查询结果
            while (resultSet.next()) {
                // 检索数据
                String dt = resultSet.getString("dt");
                String userId = resultSet.getString("user_id");
                String searchWord = resultSet.getString("search_word");
                String url = resultSet.getString("url");
                LoadEst loadEst = new LoadEst(dt, userId, searchWord, url);
                list.add(loadEst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    public void add(LoadEst loadEst) {
        // 创建连接
        try (Connection connection = JDBC.getConnection();) {
            String insertQuery = "INSERT INTO test1 (dt, user_id, search_word, url) VALUES (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(insertQuery);
            LocalTime currentTime = LocalTime.now();
            // 提取时分秒
            int hour = currentTime.getHour();
            int minute = currentTime.getMinute();
            int second = currentTime.getSecond();
            String time = "" + hour + minute + second;
            ps.setString(1, time);
            ps.setString(2,loadEst.getUserId());
            ps.setString(3,loadEst.getSearchWord());
            ps.setString(4,loadEst.getUrl());
            ps.executeUpdate();
            System.out.println("Insert successful");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public void delete(Integer id) {
        try (Connection connection = JDBC.getConnection();) {
            String deleteQuery = "DELETE FROM test1 WHERE user_id = ?";
            PreparedStatement ps = connection.prepareStatement(deleteQuery);
            ps.setString(1, String.valueOf(id));
            int rowsDeleted = ps.executeUpdate();

            if (rowsDeleted != 0) {
                System.out.println("Delete successful");
            } else {
                System.out.println("No rows were deleted. User with ID " + id + " not found.");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public void update(LoadEst loadEst){
        // 创建连接
        try (Connection connection = JDBC.getConnection();) {
            String insertQuery = "update test1 set dt = ?, search_word = ?, url = ? where user_id = ?";
            PreparedStatement ps = connection.prepareStatement(insertQuery);
            LocalTime currentTime = LocalTime.now();
            // 提取时分秒
            int hour = currentTime.getHour();
            int minute = currentTime.getMinute();
            int second = currentTime.getSecond();
            String time = "" + hour + minute + second;
            ps.setString(1, time);
            ps.setString(2,loadEst.getSearchWord());
            ps.setString(3,loadEst.getUrl());
            ps.setString(4,loadEst.getUserId());
            ps.executeUpdate();
            System.out.println("update successful");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
复制代码

 

posted @   一个小虎牙  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示