总结

在 Java 中查询 PostgreSQL 数据库

Miller Trujillo

Staff Software Engineer

将 Java 连接到 PostgreSQL

  • 设置 PostgreSQL 的 JDBC
  • 执行 SQL 查询
  • 使用 try-with-resources 管理连接
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/library");
config.setUsername("postgres");
config.setPassword("secret");

HikariDataSource ds = new HikariDataSource(config); try (Connection conn = ds.getConnection()) { ... }
在 Java 中查询 PostgreSQL 数据库

高级查询技巧

  • 用 PreparedStatement 防止 SQL 注入
  • 过滤与排序数据
  • 使用连接与子查询

$$

String sql = "SELECT * FROM books WHERE title = ? and publication_year = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "Alice in Wonderland");
pstmt.setInt(2, 1998);
ResultSet rs = pstmt.executeQuery();
在 Java 中查询 PostgreSQL 数据库

高级数据检索与管理

  • 聚合与分组
  • 事务与批处理
  • 处理大对象(LOB)
Connection conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD)
conn.setAutoCommit(false);
try (...) {
    // Execute your SQL statements here
    conn.commit();
} catch (SQLException e) {
  conn.rollback();
}
在 Java 中查询 PostgreSQL 数据库

恭喜!

在 Java 中查询 PostgreSQL 数据库

Preparing Video For Download...