Ch 3 link is at the bottom of the page
UNIT - 2 JDBC
JDBC (Java Database Connectivity) provides an API to interact with relational databases from Java applications.
Key Components of JDBC Architecture
DriverManager:
- Manages a list of database drivers.
- Establishes a connection between Java applications and the database.
Driver:
- A specific implementation for a database (e.g., MySQL, Oracle).
- Converts JDBC calls into database-specific calls.
Connection:
- Represents a session between the Java application and the database.
- Used to execute SQL queries.
Statement:
- Executes SQL queries.
- Types include
Statement
,PreparedStatement
, andCallableStatement
.
ResultSet:
- Represents the result of a query.
- Provides methods to navigate and retrieve data.
SQLException:
- Handles database access errors.
JDBC Process Workflow
- Load the JDBC driver.
- Establish a database connection.
- Create and execute SQL statements.
- Process results using
ResultSet
. - Close the connection.
Click here for more IMP AND EXAMPLES 👇
2. JDBC Drivers
JDBC drivers translate Java calls into database-specific calls. There are four types:
Type-1 (JDBC-ODBC Bridge Driver):
- Uses ODBC to connect to the database.
- Advantage: Simple to use.
- Disadvantage: Platform-dependent, slow.
Type-2 (Native-API Driver):
- Uses native libraries of the database.
- Advantage: Better performance than Type-1.
- Disadvantage: Requires native library installation.
Type-3 (Network Protocol Driver):
- Communicates via a middle-tier server that converts JDBC calls to database-specific calls.
- Advantage: Database-independent.
- Disadvantage: Requires a middle-tier server.
Type-4 (Thin Driver):
- Directly converts JDBC calls into database-specific calls.
- Advantage: Platform-independent, best performance.
- Disadvantage: Specific to a database.
3. JDBC Connection Class
The Connection
interface in JDBC represents a session with the database.
Common Methods:
createStatement()
- Creates a
Statement
object for executing SQL queries.
- Creates a
prepareStatement(String sql)
- Creates a
PreparedStatement
object for parameterized queries.
- Creates a
setAutoCommit(boolean autoCommit)
- Enables or disables transaction auto-commit mode.
commit()
- Commits a transaction.
rollback()
- Rolls back a transaction.
Example:
4. JDBC Statement Interface
The Statement
interface is used to execute static SQL queries.
Common Methods:
executeQuery(String sql)
- Executes a SELECT query.
- Returns a
ResultSet
.
executeUpdate(String sql)
- Executes INSERT, UPDATE, or DELETE queries.
- Returns the number of rows affected.
execute(String sql)
- Executes any SQL statement.
Example:
5. The ResultSet Interface
The ResultSet
interface holds the data retrieved from the database.
Key Methods:
next()
- Moves the cursor to the next row.
getString(int columnIndex)
/getString(String columnLabel)
- Retrieves the value of the specified column as a
String
.
- Retrieves the value of the specified column as a
getInt(int columnIndex)
/getInt(String columnLabel)
- Retrieves the value of the specified column as an
int
.
- Retrieves the value of the specified column as an
Example:
6. Creating a Simple JDBC Application
Steps:
- Load the driver:
- Establish a connection:
- Create and execute a query:
- Process the results:
- Close the connection:
7. Hibernate Architecture
Hibernate is an ORM (Object-Relational Mapping) tool for Java that simplifies database interactions.
Components of Hibernate Architecture:
Configuration:
- Contains configuration details like database URL, username, password, and mappings.
SessionFactory:
- A thread-safe object that creates
Session
objects. - Initialized once per application.
- A thread-safe object that creates
Session:
- Represents a unit of work.
- Used to interact with the database (e.g., save, fetch).
Transaction:
- Manages database transactions.
Query/Criteria:
- Provides methods for querying the database.
Hibernate Dialect:
- Translates HQL (Hibernate Query Language) to SQL.
Workflow:
- Configure Hibernate (
hibernate.cfg.xml
). - Obtain a
SessionFactory
. - Open a
Session
. - Begin a transaction.
- Perform database operations.
- Commit or roll back the transaction.
- Close the session.
8. Hibernate Mapping File and Java Beans
Mapping File:
- Defines the relationship between Java classes (entities) and database tables.
- Written in XML or annotations.
Example Mapping File (Employee.hbm.xml
):
Java Bean (POJO):
A simple Java class with private fields and public getters/setters.
Example Java Bean:
Post a Comment