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

JDBC_in_Java

Uploaded by

Prasanta Agasti
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)
6 views

JDBC_in_Java

Uploaded by

Prasanta Agasti
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/ 11

@Pravanjan_17p

JDBC
In
Advance Java
Definition of
JDBC CONNECTIVITY

What is JDBC connection

JDBC

JDBC (Java Database Connectivity) is a key API in


Advanced Java that allows Java applications to interact
with databases. It provides a standard way to connect
to a database, execute SQL queries, and retrieve results.
Below is a comprehensive overview:

@pravanjan_17p
KEY COMPONENTS
OF JDBC

DRIVERMANAGER:

01 MANAGES A LIST OF DATABASE DRIVERS


AND ESTABLISHES A CONNECTION BETWEEN
THE APPLICATION AND THE DATABASE.

CONNECTION:
REPRESENTS THE SESSION BETWEEN THE
JAVA APPLICATION AND THE DATABASE.
02

STATEMENT:
03 EXECUTES STATIC SQL QUERIES AGAINST
THE DATABASE.
PREPAREDSTATEMENT:
USED TO EXECUTE PARAMETERIZED SQL
QUERIES, IMPROVING SECURITY AND
04
PERFORMANCE.

CALLABLESTATEMENT:

05 USED FOR EXECUTING STORED PROCEDURES


IN THE DATABASE

RESULTSET:
REPRESENTS THE RESULTS OF A QUERY,
ALLOWING NAVIGATION THROUGH ROWS OF
06
DATA.
JAVA-JDBC

STEPS TO USE
JDBC
LOAD THE DRIVER

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

ESTABLISH A CONNECTION

Connection con = DriverManager.getConnection(


"jdbc:mysql://localhost:3306/yourDatabaseName",
"username", "password");

@PRAVANJAN_17P
CREATE A STATEMENT

Statement stmt = con.createStatement();

EXECUTE A QUERY

ResultSet rs = stmt.executeQuery("SELECT * FROM


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

CLOSE THE CONNECTION

rs.close();
stmt.close();
con.close();

@PRAVANJAN_17P
TYPE 1: TYPE 2:
JDBC-ODBC
NATIVE-API
BRIDGE DRIVER
(DEPRECATED) DRIVER

TYPES OF JDBC
DRIVERS

TYPE 3: TYPE 4:
THIN DRIVER (PURE
NETWORK
JAVA DRIVER,
PROTOCOL DRIVER WIDELY USED)

@PRAVANJAN_17P
JAVA-JDBC

COMMON JDBC
OPERATIONS
INSERT DATA:

String query = "INSERT INTO employees (id, name) VALUES (?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, 101);
pstmt.setString(2, "John Doe");
pstmt.executeUpdate();

@PRAVANJAN_17P
UPDATE DATA:

String query = "UPDATE employees SET name = ? WHERE id = ?";


PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Jane Doe");
pstmt.setInt(2, 101);
pstmt.executeUpdate();

DELETE DATA:

String query = "DELETE FROM employees WHERE id = ?";


PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setInt(1, 101);
pstmt.executeUpdate();

@PRAVANJAN_17P
Pravanjan.java

import java.sql.*;
public class JdbcExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/yourDatabase";
String user = "root";
String password = "password";
try (Connection con = DriverManager.getConnection(url, user,
password);
PreparedStatement pstmt = con.prepareStatement("SELECT
* FROM yourTable")) {

ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ",
Name: " + rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace(); } } }

#Quote #Programming #Selfcare

JDBC CONNECTION
Did you like it?
follow for more!

Like Comment Share Save

@pravanjan_17p

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy