Java Database Connectivity (JDBC) ppt by Aamir Rafique.pptx
AamirRafique14
7 views
28 slides
May 17, 2025
Slide 1 of 28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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...
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 RealWorld use cases of JDBC is also included.
Size: 1.97 MB
Language: en
Added: May 17, 2025
Slides: 28 pages
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)
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