
JSON/JSONB:
pstmt.setFetchSize(1000); // avoids memory overflow ✅ in development ✅ Close ResultSet , Statement , Connection (try-with-resources handles) ✅ Use currentSchema to avoid schema qualification ✅ Monitor with pg_stat_activity ✅ Set ApplicationName for debugging 8.3 Debugging Connection Issues // Enable driver logging java.util.logging.Logger.getLogger("org.postgresql").setLevel(Level.FINE); // Or JVM args -Dorg.postgresql.forceLogger=java.util.logging -Djava.util.logging.config.file=logging.properties 8.4 Connection Validation // Check if connection is alive if (!conn.isValid(5)) // timeout 5 seconds conn = dataSource.getConnection(); // reconnect postgres jdbc driver
conn.setAutoCommit(false); try // multiple operations conn.commit(); catch (SQLException e) conn.rollback(); finally conn.setAutoCommit(true); JSON/JSONB: pstmt
try (Connection conn = DriverManager.getConnection(url, user, password)) System.out.println("Connected!"); catch (SQLException e) e.printStackTrace(); It translates JDBC calls into PostgreSQL's wire protocol
<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);
A type 4 JDBC driver that allows Java applications to connect to a PostgreSQL database using standard JDBC APIs. It translates JDBC calls into PostgreSQL's wire protocol (libpq).
Vuoi stampare questo PDF?