Postgres Jdbc Driver [2021] Online

Class.forName("org.postgresql.Driver"); // not required in modern Java

<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.3</version> <!-- Check for latest --> </dependency> postgres jdbc driver

| 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 | !-- Check for latest --&gt

// Use dataSource.getConnection() everywhere try (Connection conn = dataSource.getConnection()) // your code ResultSet rs = stmt.executeQuery("SELECT id

jdbc:postgresql://localhost/mydb?ssl=true&sslmode=require¤tSchema=public&connectTimeout=10&ApplicationName=MyJavaApp 4.1 Query Execution // Statement try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, name FROM users")) while (rs.next()) int id = rs.getInt("id"); String name = rs.getString("name");

with your PostgreSQL version. 10. Complete Example (Production-Ready) public class PostgresDao private final HikariDataSource dataSource; public PostgresDao() HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:postgresql://localhost:5432/mydb"); config.setUsername(System.getenv("DB_USER")); config.setPassword(System.getenv("DB_PASS")); config.setMaximumPoolSize(10); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); this.dataSource = new HikariDataSource(config);