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

DBMS Week-8 Assignment

Uploaded by

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

DBMS Week-8 Assignment

Uploaded by

yayage3355
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

JDBC (Java Database Connectivity) is an API in Java that enables applications to interact with

databases. It provides methods for querying and updating data in a database, enabling Java
applications to connect to virtually any kind of relational database, such as MySQL, Oracle,
PostgreSQL, etc. JDBC acts as a bridge between Java applications and database management systems
(DBMS).

How JDBC Works

JDBC works by using drivers, which are Java classes that implement the JDBC API. Each database
vendor provides a JDBC driver that allows Java applications to communicate with its database in a
standard way. When a Java application sends SQL statements through JDBC, the driver translates
these requests into a format that the DBMS can understand, executes them, and returns the results.

Steps for Setting up a JDBC Connection

Setting up a JDBC connection involves the following steps:

1. Load the JDBC Driver: The driver is required to establish communication with the database.
Loading it enables the Java application to interact with the database.

Class.forName("com.mysql.cj.jdbc.Driver"); // Example for MySQL

This line dynamically loads the driver class. For modern JDBC (Java 6 and above), the driver is
automatically loaded when the connection URL is specified, so this step is often optional.

2. Establish the Connection: Using the DriverManager.getConnection() method, you connect to


the database. The method requires the database URL, username, and password as
parameters.

String url = "jdbc:mysql://localhost:3306/mydatabase";

String username = "root";

String password = "password";

Connection connection = DriverManager.getConnection(url, username, password);

3. Create a Statement: After establishing the connection, you create a Statement or


PreparedStatement object to execute SQL queries.

Statement statement = connection.createStatement();

Alternatively, a PreparedStatement can be used for executing parameterized queries, improving


security by preventing SQL injection.

4. Execute the Query: Use the Statement object to execute queries. Queries that return a result
set (e.g., SELECT) use the executeQuery() method, while updates (e.g., INSERT, UPDATE,
DELETE) use the executeUpdate() method.

ResultSet resultSet = statement.executeQuery("SELECT * FROM users");

5. Process the Results: If the query returns results (e.g., with SELECT), these are stored in a
ResultSet. The data in the ResultSet can be accessed using methods like next(), getString(),
etc.

while (resultSet.next()) {
System.out.println("Username: " + resultSet.getString("username"));

6. Close the Connection: To avoid resource leaks, it’s essential to close the ResultSet,
Statement, and Connection objects when they are no longer needed.

resultSet.close();

statement.close();

connection.close();

Example Code for JDBC Connection

Here’s a basic example demonstrating these steps:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class JDBCExample {

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/mydatabase";

String username = "root";

String password = "password";

try {

// Step 1: Load the JDBC Driver (optional for Java 6+)

Class.forName("com.mysql.cj.jdbc.Driver");

// Step 2: Establish the Connection

Connection connection = DriverManager.getConnection(url, username, password);

// Step 3: Create a Statement

Statement statement = connection.createStatement();

// Step 4: Execute a Query


ResultSet resultSet = statement.executeQuery("SELECT * FROM users");

// Step 5: Process the Results

while (resultSet.next()) {

System.out.println("User: " + resultSet.getString("username"));

// Step 6: Close the Connection

resultSet.close();

statement.close();

connection.close();

} catch (Exception e) {

e.printStackTrace();

Summary

JDBC provides a standardized way for Java applications to interact with databases by using drivers
that translate Java requests into database commands. The process involves loading the driver,
establishing a connection, creating statements, executing queries, processing results, and then
closing the connection to manage resources effectively.

You might also like