2024年9月20日
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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构