0% found this document useful (0 votes)
4 views

notes cs

The document provides concise notes on JDBC (Java Database Connectivity), explaining its purpose as an API for database interaction and outlining key components such as DriverManager, Connection, and Statement. It details the steps to connect to a database, methods for executing queries, and concepts like batch execution, stored procedures, and metadata. Additionally, it includes practical examples for creating a Maven project and implementing connection pooling.

Uploaded by

derotav936
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

notes cs

The document provides concise notes on JDBC (Java Database Connectivity), explaining its purpose as an API for database interaction and outlining key components such as DriverManager, Connection, and Statement. It details the steps to connect to a database, methods for executing queries, and concepts like batch execution, stored procedures, and metadata. Additionally, it includes practical examples for creating a Maven project and implementing connection pooling.

Uploaded by

derotav936
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

hay deep ai i am a student of computer science

and i want to learn JDBC but right now i need


notes or i am sharing you with you a sample pdf
but i expect better then this pdf , please use simple
language to make this and make create best notes
for me, i share with you topics also make notes
topic wise
Here are concise and easy-to-understand notes on JDBC, organized topic-wise based on the
shared image:

2.1 Introduction to JDBC


JDBC (Java Database Connectivity) is an API that allows Java applications to interact with
databases.
It provides methods to query and update data in a database.
JDBC acts as a bridge between Java applications and databases.

2.2 Difference Between File and Database


File Database

Stores data in a flat structure. Stores data in a structured format (tables).

No querying mechanism. Supports querying using SQL.

Limited data integrity. Ensures data consistency and integrity.

Slower for large datasets. Optimized for large datasets.

2.3 What is JDBC API


A set of classes and interfaces in Java for database operations.
Key components:
DriverManager: Manages database drivers.
Connection: Establishes a connection to the database.
Statement and PreparedStatement: Execute SQL queries.
ResultSet: Retrieves query results.

2.4 5 Steps to Connect to Database


1. Load the JDBC driver:
Class.forName("com.mysql.cj.jdbc.Driver");

2. Establish a connection:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user"

3. Create a statement:
Statement stmt = con.createStatement();

4. Execute queries:
ResultSet rs = stmt.executeQuery("SELECT * FROM table_name");

5. Close the connection:


con.close();

2.5 Create Statement Using: Statement


Statement is used for executing static SQL queries.
Example:
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table_name");

2.6 Create Statement Using: PreparedStatement


PreparedStatement is used for dynamic or parameterized queries.
Example:
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM table_name WHERE id = ?
pstmt.setInt(1, 101);
ResultSet rs = pstmt.executeQuery();
2.7 Execute Query Methods
1. execute(): Executes any SQL statement (query or update).
2. executeQuery(): Executes SELECT queries and returns a ResultSet.
3. executeUpdate(): Executes INSERT, UPDATE, DELETE queries and returns the number of
affected rows.

2.8 Batch Execution


Used to execute multiple SQL statements as a batch for better performance.
Example:
Statement stmt = con.createStatement();
stmt.addBatch("INSERT INTO table_name VALUES (1, 'A')");
stmt.addBatch("INSERT INTO table_name VALUES (2, 'B')");
stmt.executeBatch();

2.9 Create Maven Project for CRUD Operations


1. Add dependencies in pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.x.x</version>
</dependency>

2. Write Java code for Create, Read, Update, Delete operations using JDBC.

2.10 Create Procedure in DB


A procedure is a stored program in the database.
Example:
CREATE PROCEDURE GetUsers()
BEGIN
SELECT * FROM users;
END;

2.11 Create Function in DB


A function returns a value after execution.
Example:
CREATE FUNCTION GetUserCount()
RETURNS INT
BEGIN
RETURN (SELECT COUNT(*) FROM users);
END;

2.12 What is DB Metadata


Metadata provides information about the database (e.g., tables, columns).
Accessed using DatabaseMetaData object:
DatabaseMetaData dbmd = con.getMetaData();
System.out.println(dbmd.getDatabaseProductName());

2.13 What is ResultSet Metadata


Provides information about the columns in a ResultSet.
Accessed using ResultSetMetaData object:
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println(rsmd.getColumnName(1));

2.14 Create Connection Pool Practical


Connection pooling improves performance by reusing connections.
Use libraries like Apache DBCP or HikariCP.
Example with HikariCP:
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/db");
config.setUsername("user");
config.setPassword("password");

HikariDataSource ds = new HikariDataSource(config);

Connection con = ds.getConnection();

Let me know if you need further elaboration on any topic!


You might also like