0% found this document useful (0 votes)
36 views21 pages

Database Connectivity Java

Uploaded by

vinaymane6362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views21 pages

Database Connectivity Java

Uploaded by

vinaymane6362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Reva university

Database
Connectivity Steps

"Empowering Java Applications to Interact Seamlessly with Databases"


Database Connectivity in Java

1 Sai Kumar [R23DE221]

2 Chandra Shekhar [R23DE237]

3 Rayees Ahamad [R23DE208]

Srinath KR [R23DE196]
4
Introduction to JDBC

What is JDBC?
• JDBC stands for Java Database Connectivity.
• It is an API used to connect Java applications with databases.
• Allows us to perform operations like:
Connecting to a database.
Executing SQL queries.
Retrieving and processing results.
Project Setup in Java

Preparing the Java Project

1. Create a new Java project in your IDE.


2. Add the JDBC driver to your project:
• Go to Project Settings → Libraries → Add JAR/Folder → Select the
driver file.
3. Verify that your project can recognize the driver.
Output of this Step:
• Database is ready.
• Java project is set up with JDBC support.
Requirements for Database Connectivity

What Do You Need?


Database Software: Install any relational database like
MySQL, PostgreSQL, or Oracle.
JDBC Driver: A specific driver is needed to connect the Java
application to the database. Examples:
• MySQL: mysql-connector-java.jar.
• PostgreSQL: postgresql-<version>.jar.
Java IDE: Use tools like Eclipse, IntelliJ IDEA, or NetBeans for
coding.
Setting Up the Database

Create the Database and Table


• Use MySQL (or any database tool) to create a sample database:sqlCopy
code

• Result: A database with one table (passengers) is ready.


Database Connectivity

Why Establish a Database Connection?


• To enable Java applications to interact with databases.
• Essential for performing CRUD operations:
Create (Insert Data)
Read (Retrieve Data)
Update (Modify Data)
Delete (Remove Data)
Example: Connecting to a MySQL database using JDBC.
Required Libraries

Import Required Libraries


Use the java.sql package for database operations:

Why is it Needed?
Provides essential classes like Connection, DriverManager,and
SQLException.
Loading the JDBC Driver

Load the JDBC Driver


In older versions, the driver must be explicitly loaded:

Purpose: Ensures the Java application recognizes the database


driver.

Note: For Java 8 or later, this step is optional as drivers are


auto-loaded.
Establish the Connection

Use DriverManager to Connect


• Provide the database URL, username, and password:

Output:
• Successful connection establishes communication between
Java and the database.
Handling Errors

Error Handling with try-catch


Wrap the connection code in try-catch blocks to handle
exceptions:

Why Handle Errors?


• Ensures smooth application behavior and proper debugging.
Query Execution

What Are SQL Queries?


• SQL queries are used to perform operations on the database:
INSERT: Add new records.
SELECT: Retrieve data.
UPDATE: Modify existing data.
DELETE: Remove records.
• These queries interact with the database after the connection
is established.
Executing SQL Queries

Using Statement Interface


• Create a Statement object from the connection

Execute queries like INSERT, UPDATE, DELETE


Example for INSERT:
String sql = "INSERT INTO passengers (name, email,
contact_number) VALUES ('John Doe', '[email protected]',
'1234567890')";
stmt.executeUpdate(sql);
Prepared Statements

Why Use PreparedStatement?


Safer for dynamic and parameterized queries.
Prevents SQL injection.
Example:
Retrieving Data with SELECT Queries

Using executeQuery
For retrieving data:

Use ResultSet to process rows and access column values.


Closing the Database Connection

Why Close the Database Connection?

• Releases system resources (memory, CPU).


• Prevents memory leaks and resource contention.
• Ensures smooth database performance and stability.
• Avoids blocking other processes from accessing the database.

.
Steps to Close the Database Connection

Closing the Connection


Close the Connection object:

Frees up the resources held by the connection.


Close the Statement and ResultSet objects:
try-with-resources

Automatic Resource Management


Java 7 and later supports try-with-resources for automatic closure:

Benefits:
• Resources are automatically closed at the end of the try block.
• Cleaner and more maintainable code.
Errors While Closing

Error Handling During Connection:


Use try-catch to handle exceptions when closing resources:
Best Practices for Closing Connections

Best Practices
• Close in reverse order:
Close ResultSet → Statement → Connection.
• Always use try-with-resources to simplify code and avoid
forgetting closures.
• Avoid using global or long-lived connections.
Thank you !

You might also like