JDBC / NOTES</>↗JDBC14 Jan 2026
Executing a simple SQL query using a Statement object in JDBC follows a straightforward pattern: establish a connection, create the statement, execute the query, and process the results. Here is a clean example of how to perform a SELECT query: Simple SQL Query Example package org.kodejava.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import […]
JDBC / NOTES</>↗JDBC14 Jan 2026
Properly closing JDBC resources is crucial to prevent memory leaks and database connection exhaustion. In modern Java, the absolute best way to do this is by using the try-with-resources statement. The Best Practice: Try-with-Resources Introduced in Java 7, this approach automatically closes any resource that implements java.lang.AutoCloseable (which Connection, Statement, and ResultSet all do) at […]
JNDI / NOTES</>↗JNDI13 Jan 2026
Switching from DriverManager to DataSource is a best practice in modern Java applications because it supports connection pooling, is more configurable, and decouples your code from the specific database driver implementation. While DriverManager creates a physical connection every time you call getConnection(), a DataSource (specifically a pooling one) maintains a set of open connections that […]
JDBC / NOTES</>↗JDBC13 Jan 2026
Creating a database connection with DriverManager is the standard way to establish a session with a database in JDBC. 1. The Essential Formula To get a connection, you call DriverManager.getConnection() using a Connection URL, a username, and a password. Connection connection = DriverManager.getConnection(url, username, password); 2. Implementation Example In modern Java (JDBC 4.0+), you don’t […]
JDBC / NOTES</>↗JDBC12 Jan 2026
In modern Java (specifically JDBC 4.0 and later), you generally do not need to write code to load the driver. The DriverManager uses the Service Provider Interface (SPI) to automatically discover and load any JDBC drivers present on your classpath. 1. The Modern Way: Automatic Discovery As long as the driver JAR is on your […]
JDBC / NOTES</>↗JDBC12 Jan 2026
To connect to Microsoft SQL Server using JDBC, you’ll need the Microsoft JDBC Driver for SQL Server. Using Maven, you can add the dependency and use the DriverManager to establish a connection. Here is the step-by-step process: 1. Add the Dependency Add the following dependency to your pom.xml file: <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>12.8.1.jre11</version> <!– Check […]