Postgres Jdbc Driver -
Properties props = new Properties(); props.setProperty("user", "myuser"); props.setProperty("password", "mypass"); props.setProperty("ssl", "true"); props.setProperty("connectTimeout", "10"); Connection conn = DriverManager.getConnection(url, props); 3. Essential Connection Parameters | Parameter | Description | Example | |-----------|-------------|---------| | ssl | Enable SSL/TLS | true | | sslmode | SSL mode: disable, allow, prefer, require, verify-ca, verify-full | require | | currentSchema | Set default schema | public,myapp | | connectTimeout | Socket connect timeout (seconds) | 30 | | socketTimeout | Socket read timeout (seconds) | 60 | | tcpKeepAlive | Enable TCP keepalive | true | | loginTimeout | Max time for connection (seconds) | 20 | | ApplicationName | Identify app in pg_stat_activity | MyApp | | prepareThreshold | Number of executes before switching to server-prepared | 5 | | preparedStatementCacheQueries | Max cached queries per connection | 256 |
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; postgres jdbc driver
conn.setAutoCommit(false); try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO logs (msg) VALUES (?)")) for (String msg : messages) pstmt.setString(1, msg); pstmt.addBatch(); int[] results = pstmt.executeBatch(); conn.commit(); catch (SQLException e) conn.rollback(); Properties props = new Properties(); props