String sql = "SELECT id, name FROM users WHERE email = ?"; try (PreparedStatement pstmt = conn.prepareStatement(sql)) pstmt.setString(1, "alice@example.com"); ResultSet rs = pstmt.executeQuery(); while (rs.next()) long id = rs.getLong("id"); String name = rs.getString("name");
This article explores how to effectively use the official driver, covering setup, CRUD operations, connection pooling, and advanced features like LISTEN / NOTIFY and COPY . The PostgreSQL JDBC Driver (Group ID: org.postgresql , Artifact ID: postgresql ) is a Type 4 JDBC driver. This means it’s written purely in Java and connects directly to the database using the PostgreSQL wire protocol—no native libraries or ODBC bridges required. postgresql java driver
PostgreSQL supports asynchronous messaging. The JDBC driver can listen for notifications. String sql = "SELECT id, name FROM users WHERE email =
String url = "jdbc:postgresql://localhost:5432/mydb"; Properties props = new Properties(); props.setProperty("user", "postgres"); props.setProperty("password", "secret"); props.setProperty("ssl", "true"); try (Connection conn = DriverManager.getConnection(url, props)) System.out.println("Connected to PostgreSQL!"); catch (SQLException e) e.printStackTrace(); PostgreSQL supports asynchronous messaging
HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:postgresql://localhost:5432/mydb"); config.setUsername("postgres"); config.setPassword("secret"); config.setMaximumPoolSize(10); config.setConnectionTimeout(30000); try (HikariDataSource dataSource = new HikariDataSource(config); Connection conn = dataSource.getConnection()) // Use connection