DIPLOMA CH 2 MOST IMP QUESTION (AJP)

 
Ch 3 link is at the bottom of the page


UNIT - 2 JDBC



1. JDBC ARCHITECTURE
2.JDBC DRIVERS
3.JDBC CONNECTION CLASS
4.JDBC STATEMENT INTERFACE
5. THE RESULTSET INTERFACE
6. CREATING SIMPLE JDBC APPLICATIN
7. HIBERNATE ARCHITECTURE
8.HIBERNATE MAPPING FILE AND JAVA BEANS


JDBC (Java Database Connectivity) provides an API to interact with relational databases from Java applications.

Key Components of JDBC Architecture

  1. DriverManager:

    • Manages a list of database drivers.
    • Establishes a connection between Java applications and the database.
  2. Driver:

    • A specific implementation for a database (e.g., MySQL, Oracle).
    • Converts JDBC calls into database-specific calls.
  3. Connection:

    • Represents a session between the Java application and the database.
    • Used to execute SQL queries.
  4. Statement:

    • Executes SQL queries.
    • Types include Statement, PreparedStatement, and CallableStatement.
  5. ResultSet:

    • Represents the result of a query.
    • Provides methods to navigate and retrieve data.
  6. SQLException:

    • Handles database access errors.

JDBC Process Workflow

  1. Load the JDBC driver.
  2. Establish a database connection.
  3. Create and execute SQL statements.
  4. Process results using ResultSet.
  5. 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:

  1. Type-1 (JDBC-ODBC Bridge Driver):

    • Uses ODBC to connect to the database.
    • Advantage: Simple to use.
    • Disadvantage: Platform-dependent, slow.
  2. Type-2 (Native-API Driver):

    • Uses native libraries of the database.
    • Advantage: Better performance than Type-1.
    • Disadvantage: Requires native library installation.
  3. 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.
  4. 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:

  1. createStatement()

    • Creates a Statement object for executing SQL queries.
  2. prepareStatement(String sql)

    • Creates a PreparedStatement object for parameterized queries.
  3. setAutoCommit(boolean autoCommit)

    • Enables or disables transaction auto-commit mode.
  4. commit()

    • Commits a transaction.
  5. rollback()

    • Rolls back a transaction.

Example:


Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb", "username", "password");


Click here for more IMP AND EXAMPLES 👇



4. JDBC Statement Interface

The Statement interface is used to execute static SQL queries.

Common Methods:

  1. executeQuery(String sql)

    • Executes a SELECT query.
    • Returns a ResultSet.
  2. executeUpdate(String sql)

    • Executes INSERT, UPDATE, or DELETE queries.
    • Returns the number of rows affected.
  3. execute(String sql)

    • Executes any SQL statement.

Example:


Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users");


Click here for more IMP AND EXAMPLES 👇



5. The ResultSet Interface

The ResultSet interface holds the data retrieved from the database.

Key Methods:

  1. next()

    • Moves the cursor to the next row.
  2. getString(int columnIndex) / getString(String columnLabel)

    • Retrieves the value of the specified column as a String.
  3. getInt(int columnIndex) / getInt(String columnLabel)

    • Retrieves the value of the specified column as an int.

Example:


while (rs.next()) { System.out.println(rs.getString("username")); }

Click here for more IMP AND EXAMPLES 👇



6. Creating a Simple JDBC Application

Steps:

  1. Load the driver:

    Class.forName("com.mysql.cj.jdbc.Driver");
  2. Establish a connection:

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");
  3. Create and execute a query:

    Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users");
  4. Process the results:

    while (rs.next()) { System.out.println(rs.getString("username")); }
  5. Close the connection:

    con.close();

7. Hibernate Architecture

Hibernate is an ORM (Object-Relational Mapping) tool for Java that simplifies database interactions.

Components of Hibernate Architecture:

  1. Configuration:

    • Contains configuration details like database URL, username, password, and mappings.
  2. SessionFactory:

    • A thread-safe object that creates Session objects.
    • Initialized once per application.
  3. Session:

    • Represents a unit of work.
    • Used to interact with the database (e.g., save, fetch).
  4. Transaction:

    • Manages database transactions.
  5. Query/Criteria:

    • Provides methods for querying the database.
  6. Hibernate Dialect:

    • Translates HQL (Hibernate Query Language) to SQL.

Workflow:

  1. Configure Hibernate (hibernate.cfg.xml).
  2. Obtain a SessionFactory.
  3. Open a Session.
  4. Begin a transaction.
  5. Perform database operations.
  6. Commit or roll back the transaction.
  7. 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):


<hibernate-mapping> <class name="com.example.Employee" table="employee"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name"/> <property name="salary" column="salary"/> </class> </hibernate-mapping>

Java Bean (POJO):

A simple Java class with private fields and public getters/setters.

Example Java Bean:

package com.example; public class Employee { private int id; private String name; private double salary; // Getters and setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }


Click here for more IMP FOR SUBJECTS 👇




CH 3 IMP






Post a Comment

Previous Post Next Post