2024年9月20日

chrisrmas、發表於2024-09-21
import com.std.util.HiveUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class TestInsertDat {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        Scanner scanner = new Scanner(System.in);

        try {
            // 1. Establish connection using DatabaseConnection class
            conn = HiveUtil.getConnection();

            // 2. SQL to retrieve data
            String sqlSelect = "SELECT * FROM test";

            // 3. Create SQL statement executor
            stmt = conn.createStatement();

            // 4. Execute SQL --> get a result set (multiple rows)
            rs = stmt.executeQuery(sqlSelect);

            // 5. Process result set
            System.out.println("編號 \t" + " 姓名 \t" + "   性別");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                System.out.println(id + "\t\t" + name + "\t\t" + gender);
            }

            // 6. Adding new data
            System.out.println("請輸入要新增的姓名和性別(格式: 姓名 性別)");
            String input = scanner.nextLine();
            String[] parts = input.split(" ");
            if (parts.length == 3) {
                String id = parts[0];
                String name = parts[1];
                String gender = parts[2];

                // Prepare INSERT SQL query
//                String sqlInsert = "INSERT INTO test (name, gender) VALUES (?, ?)";
                try {
                    // Prepare INSERT SQL query
                    String sqlInsert = "INSERT INTO test (id,name, gender) VALUES (?,?, ?)";
                    try (PreparedStatement pstmt = conn.prepareStatement(sqlInsert)) {
                        pstmt.setString(1, id);
                        pstmt.setString(2, name);
                        pstmt.setString(3, gender);
                        int rowsAffected = pstmt.executeUpdate();

                        System.out.println("成功插入 " + rowsAffected + " 條記錄。");
                    } catch (SQLException e) {
                        System.err.println("插入資料時發生錯誤: " + e.getMessage());
                        e.printStackTrace(); // 列印堆疊跟蹤以獲得更多上下文
                    }
                } catch (Exception e) {
                    System.err.println("整體操作失敗: " + e.getMessage());
                    e.printStackTrace();
                }

            } else {
                System.out.println("輸入格式不正確,請使用: 姓名 性別");
            }

            // Re-fetch data to show the new record
            rs = stmt.executeQuery(sqlSelect);
            System.out.println("新增後的資料:");
            System.out.println("編號 \t" + " 姓名 \t" + "   性別");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                System.out.println(id + "\t\t" + name + "\t\t" + gender);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // Close resources in reverse order of opening
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
                scanner.close(); // Close scanner
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class HiveUtil {
    // Database URL, username, and password
    private static final String URL = "jdbc:hive2://node1:10000";
    private static final String USER = "hadoop";
    private static final String PASS = "";

    // Method to create and return a database connection
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASS);
    }
}