Postgres Jdbc Driver ((new)) Official
<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);
// Store JSON PGobject jsonObj = new PGobject(); jsonObj.setType("json"); jsonObj.setValue("\"key\": \"value\""); pstmt.setObject(1, jsonObj); // Read JSON String jsonStr = rs.getString("data");
// Use dataSource.getConnection() everywhere try (Connection conn = dataSource.getConnection()) // your code postgres jdbc driver
<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.3</version> <!-- Check for latest --> </dependency>
// PreparedStatement (preferred) String sql = "SELECT * FROM users WHERE age > ? AND city = ?"; try (PreparedStatement pstmt = conn.prepareStatement(sql)) pstmt.setInt(1, 18); pstmt.setString(2, "Paris"); ResultSet rs = pstmt.executeQuery(); <dependency> <groupId>org
// Store array Integer[] intArray = 1, 2, 3; Array array = conn.createArrayOf("integer", intArray); pstmt.setArray(1, array); // Read array Array resultArray = rs.getArray("int_array"); Integer[] values = (Integer[]) resultArray.getArray();
pstmt.setObject(1, UUID.randomUUID()); UUID id = rs.getObject("id", UUID.class); 5.1 HikariCP (Best Performance) <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>5.1.0</version> </dependency> HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:postgresql://localhost/mydb"); config.setUsername("user"); config.setPassword("pass"); config.setMaximumPoolSize(10); config.setMinimumIdle(5); config.setConnectionTimeout(30000); config.setIdleTimeout(600000); config.setPoolName("PostgresPool"); HikariDataSource dataSource = new HikariDataSource(config); AND city =
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).