Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx

AamirRafique14 7 views 28 slides May 17, 2025
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

This PowerPoint presentation contains all the information regarding Java Database Connectivity (JDBC), starting from introduction to Java programming language, Databases, SQL, JDBC architecture, Driver types, Step by step code walkthrough, statement vs prepared Statement methods etc. Moreover, the R...


Slide Content

Made by AAMIR RAFIQUE

What is Java?

Key Features of Java

What is a Database? A structured collection of data stored electronically Allows data to be easily inserted , updated , searched , and deleted Types of Databases: Relational (SQL): MySQL, Oracle, PostgreSQL Non-Relational (NoSQL): MongoDB, Firebase

What is SQL?

Why Java Needs Databases Java applications often need to store, read, or process data persistently Databases provide: Long-term data storage Data organization Efficient retrieval JDBC helps Java communicate with databases using SQL

Introduction to JDBC

Why Use JDBC? Enables Java applications to: Insert, update, delete, and read data from databases Use SQL commands through Java No need to switch between different database tools.

JDBC Architecture Two main components: JDBC API (Java application interface) JDBC Driver (translates commands to DB)

JDBC API Components

JDBC Driver Types Example: com.mysql.cj.jdbc.Driver

JDBC Connection String

Basic JDBC Connection String Format (for MySQL): String url = " jdbc:mysql ://localhost:3306/ mydatabase "; Add Credentials: String user = “admin"; String password = " mypassword "; Connection con = DriverManager.getConnection ( url , user , password ); Part Meaning jdbc Tells Java we're using JDBC mysql The database type (could also be oracle, postgresql , etc.) localhost Host where the DB is running (can also be an IP or domain) 3306 Port number (default for MySQL) mydatabase Name of the database to connect to

7 Steps of JDBC Programming

Step-by-Step: Code Walkthrough (Statement method)

import java.sql .*; public class JdbcExample { public static void main(String[] args) { try { // Step 1 & 2: Load and register driver Class.forName (" com.mysql.cj.jdbc.Driver " ); // Step 3: Establish connection Connection con = DriverManager.getConnection ( " jdbc:mysql ://localhost:3306/ mydatabase ", “admin", " mypassword " ); // Step 4: Create statement Statement stmt = con . createStatement (); // Step 5: Execute query ResultSet rs = stmt . executeQuery ( "SELECT * FROM users" ); // Step 6: Process results while ( rs . next ()) { System.out.println ( rs .getInt ( "id" ) + " - " + rs . getString ( "name" ));} // Step 7: Close connection con . close (); } catch ( Exception e ) { e . printStackTrace (); } } }

Using PreparedStatement in JDBC Why Use PreparedStatement ? Prevents SQL Injection More efficient for repeated queries Automatically handles data types Code example 

import java.sql .*; public class PreparedStatementExample { public static void main(String[] args) { try { // Step 1 & 2: Load and register driver Class.forName ( " com.mysql.cj.jdbc.Driver " ); // Step 3: Establish connection Connection con = DriverManager.getConnection ( " jdbc:mysql ://localhost:3306/ mydatabase ", “admin", " mypassword " ); // Step 4: Create PreparedStatement with a parameterized query String query = "SELECT * FROM users WHERE id = ?" ; PreparedStatement ps = con . prepareStatement ( query ); // Step 5: Set the value for the placeholder ps .setInt (1, 1); // Sets the value of "?" to 1 // Step 6: Execute query ResultSet rs = stmt .executeQuery ( "SELECT * FROM users" ); // Step 7: Process results while ( rs .next ()) { System.out.println ( rs .getInt ( "id" ) + " - " + rs .getString ( "name" ));} // Step 8: Close connection con .close (); } catch (Exception e ) { e .printStackTrace (); } } }

Statement vs PreparedStatement in JDBC Feature Statement PreparedStatement Query Structure Static, hard-coded SQL Parameterized SQL with ? Example "SELECT * FROM users WHERE id = 1" "SELECT * FROM users WHERE id = ?" Security Prone to SQL injection Prevents SQL injection Reusability No  query is compiled each time Yes  query is precompiled and reused Performance Slower for repeated queries Faster due to pre-compilation Parameter Handling Manual string concatenation Uses setInt (), setString (), etc.

Handling Exceptions

Understanding catch ( SQLException e) { e.printStackTrace (); } This block handles SQL-related errors that may occur during JDBC operations (e.g., connection failure, bad query). Code Breakdown  Code Part Explanation catch ( SQLException e) Catches any SQL error thrown in the try block SQLException A built-in Java class for handling database errors e.printStackTrace (); Prints error type, message, and where it occurred in the code (stack trace)

JDBC CRUD Operations

Closing Resources

Best Java database Practices

Real-World Uses of JDBC Web applications using Servlets/JSP Desktop apps (Swing/JavaFX) Back-end systems that talk to databases Integration with frameworks like Hibernate, Spring JDBC

Conclusion

THANK YOU