Postgres Jdbc Driver -
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
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). 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(); CopyManager copyManager = ((PGConnection) conn)
// 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(); AND city =
// Use dataSource.getConnection() everywhere try (Connection conn = dataSource.getConnection()) // your code