JDBC / NOTES</>↗JDBC11 Feb 2026
To use connection pooling in plain JDBC with HikariCP, the main shift is: stop using DriverManager.getConnection(…) everywhere create one DataSource (the pool) at startup whenever you need a DB connection, call dataSource.getConnection() always close resources with try-with-resources (closing returns the connection to the pool, it does not kill the physical connection) 1) Create a pooled […]
JDBC / NOTES</>↗JDBC20 Jan 2026
Savepoints in JDBC provide fine-grained control over transactions by allowing you to roll back to a specific point within a transaction rather than undoing everything. This is particularly useful for handling optional operations or partial failures. Key Steps to Use Savepoints Disable Auto-commit: Savepoints only work within a manual transaction. Set a Savepoint: Use connection.setSavepoint() […]
JDBC / NOTES</>↗JDBC19 Jan 2026
Using transactions in JDBC is essential when you need to ensure that a group of SQL statements either all succeed or all fail together (maintaining Atomicity). By default, a JDBC Connection is in auto-commit mode, meaning every single SQL statement is treated as its own transaction and committed immediately. To manage transactions manually, follow these […]
JDBC / NOTES</>↗JDBC19 Jan 2026
To set the fetch size for large queries in Java using JDBC, you use the setFetchSize(int rows) method on a Statement or PreparedStatement object. This gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed. This is particularly useful for large […]
JDBC / NOTES</>↗JDBC18 Jan 2026
To use auto-generated keys in JDBC (like an AUTO_INCREMENT primary key), you need to follow a three-step process: notify the statement you want the keys, execute the update, and then retrieve them from a special ResultSet. Here is a practical example using PreparedStatement: 1. Prepare the Statement When creating your PreparedStatement, you must pass the […]
JDBC / NOTES</>↗JDBC18 Jan 2026
To batch insert data with JDBC, you typically use the addBatch() and executeBatch() methods. This is much more efficient than executing individual INSERT statements because it reduces the number of round-trips between your application and the database. The most common and secure way to do this is with a PreparedStatement. Batch Insert with PreparedStatement Using […]