Postgres Jdbc Driver ((top)) -
public void close() if (dataSource != null && !dataSource.isClosed()) dataSource.close();
8.1 Common Errors | Error | Solution | |-------|----------| | The connection attempt failed | Check PostgreSQL running, firewall, pg_hba.conf | | No suitable driver found | Add JDBC jar to classpath | | FATAL: no pg_hba.conf entry | Add client IP/method to pg_hba.conf | | PSQLException: This connection has been closed | Reconnect or use connection pool | | PSQLException: Out of memory | Increase JVM heap or reduce result set size | 8.2 Best Practices Checklist ✅ Always use PreparedStatement (prevents SQL injection) ✅ Use try-with-resources for automatic closing ✅ Implement connection pooling (HikariCP) ✅ Set reasonable timeouts (connect, socket, login) ✅ Use fetchSize for large result sets postgres jdbc driver
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.11.0</version> </dependency> BasicDataSource ds = new BasicDataSource(); ds.setUrl("jdbc:postgresql://localhost/mydb"); ds.setUsername("user"); ds.setPassword("pass"); ds.setMaxTotal(20); ds.setMaxIdle(10); 6. SSL/TLS Configuration 6.1 Simple SSL (Require) jdbc:postgresql://localhost/mydb?ssl=true&sslmode=require 6.2 Certificate Validation (verify-full) Properties props = new Properties(); props.setProperty("user", "myuser"); props.setProperty("password", "mypass"); props.setProperty("ssl", "true"); props.setProperty("sslmode", "verify-full"); props.setProperty("sslrootcert", "/path/to/ca-cert.pem"); props.setProperty("sslcert", "/path/to/client-cert.pem"); props.setProperty("sslkey", "/path/to/client-key.pem"); 6.3 Disable SSL (Not recommended) jdbc:postgresql://localhost/mydb?ssl=false 7. Advanced Features 7.1 LISTEN/NOTIFY try (Statement stmt = conn.createStatement()) stmt.execute("LISTEN mychannel"); // Wait for notifications (blocking) while (true) Statement stmt2 = conn.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT 1"); rs.close(); org.postgresql.PGNotification[] notifications = ((org.postgresql.PGConnection) conn).getNotifications(); if (notifications != null) for (PGNotification note : notifications) System.out.println(note.getName() + ": " + note.getParameter()); Thread.sleep(1000); public void close() if (dataSource
| PostgreSQL | JDBC Driver Version | |------------|---------------------| | 16.x | 42.7.x | | 15.x | 42.6.x - 42.7.x | | 14.x | 42.4.x - 42.7.x | | 13.x | 42.2.x - 42.7.x | | 12.x | 42.2.x - 42.7.x | | 11.x | 42.2.x - 42.7.x | | 10.x | 42.2.x - 42.7.x | | 9.6 | 42.2.x - 42.3.x | BasicDataSource ds = new BasicDataSource()
public User findById(long id) String sql = "SELECT id, name, email FROM users WHERE id = ?"; try (Connection conn = dataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql)) pstmt.setLong(1, id); try (ResultSet rs = pstmt.executeQuery()) if (rs.next()) return new User(rs.getLong("id"), rs.getString("name"), rs.getString("email")); catch (SQLException e) throw new RuntimeException("Database error", e); return null;
CopyManager copyManager = ((PGConnection) conn).getCopyAPI(); String sql = "COPY users (name, email) FROM STDIN"; CopyIn copyIn = copyManager.copyIn(sql); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(copyIn)); writer.write("Alice\talice@example.com\n"); writer.write("Bob\tbob@example.com\n"); writer.close(); long rows = copyIn.getHandledRowCount(); 7.3 Logical Replication (PG 10+) PGConnection pgConn = conn.unwrap(PGConnection.class); PGReplicationStream stream = pgConn.getReplicationAPI() .replicationStream() .logical() .withSlotName("test_slot") .withSlotOption("proto_version", "1") .withSlotOption("publication_names", "mypub") .start(); while (true) ByteBuffer msg = stream.readPending(); if (msg != null) // process changes