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);
}
}