Sep 28, 2024•1 min read
Database & JDBC
JDBC fundamentals: core interfaces, connection APIs, prepared/callable statements, bind variables, and ResultSet behavior.
title: "Database & JDBC" date: "2024-09-28" excerpt: "JDBC fundamentals: core interfaces, connection APIs, prepared/callable statements, bind variables, and ResultSet behavior."
tags: "java,jdbc,database" source: "https://shrikant-havale.in/2024/09/28/database-jdbc/"
A concise reference for JDBC basics and common interview/exam details.
JDBC Core Concepts
- JDBC drivers provide concrete implementations for each database vendor.
- Core interfaces live under
java.sql:DriverConnectionPreparedStatementCallableStatementResultSet
JDBC URL
JDBC URLs follow:
protocol:subprotocol:subname
- Protocol is always
jdbc. - Subprotocol is typically the database name.
- Subname format depends on vendor.
Connections
DriverManager.getConnection(...) throws checked SQLException.
DriverManager.getConnection("connection-string");
DriverManager.getConnection("connection-string", "username", "password");
Statements
PreparedStatementextendsStatementand supports bind parameters.CallableStatementextendsStatementand is used for stored procedures.
PreparedStatement ps = conn.prepareStatement("update ...");
int updated = ps.executeUpdate();
PreparedStatement ps = conn.prepareStatement("select ...");
ResultSet rs = ps.executeQuery();
execute() returns a boolean:
true=> result is aResultSetfalse=> use update count
Bind Variables
- Indexing starts at
1(not0). - Missing or extra bind variables cause
SQLException.
ResultSet Safety
SQLException can occur when:
- Accessing a non-existing column name/index.
- Reading when cursor is not on a valid row.
Source
Original post: https://shrikant-havale.in/2024/09/28/database-jdbc/