ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3114Network Programming Jd…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3114

    Advanced Computer Programming

    Progress0 / 12 done
    Topics12
    1Abstract classes and Interfaces2Packages and Exception handling3Advanced issues of GUI and event handling4Applets and Swing
    5JDBC6Multithreading7Building Client/Server and implementing protocols8RMI (Remote Method Invocation)9Java Secure Socket Extension and Secure Sockets Layer (SSL)10SSL Socket and SSL Server Socket classes
    11HTTPS12Developing TCP/IP client and server with telnet
    3 min read
    Topic 5 of 12
    Topic Study Note Illustration
    05

    Network Programming Concepts: JDBC

    JDBC (Java Database Connectivity) is a Java API that allows Java programs to connect and interact with databases .

    📘 Network Programming Concepts: JDBC (Java Database Connectivity)


    🔷 1. Definition

    JDBC (Java Database Connectivity) is a Java API that allows Java programs to connect and interact with databases.

    👉 It is used to:

    • Execute SQL queries
    • Retrieve and update data
    • Communicate with databases over a network

    🔷 2. Key Concept

    JDBC acts as a bridge between:

    • Java Application
    • Database (like MySQL, Oracle)

    📊 Concept Diagram (Description) Java Program → JDBC API → JDBC Driver → Database


    🔷 3. JDBC Architecture

    🔹 Components:

    1. JDBC API

      • Provides classes and interfaces
    2. JDBC Driver Manager

      • Manages database drivers
    3. JDBC Driver

      • Converts Java calls into database-specific commands
    4. Database

      • Stores data

    🔷 4. Types of JDBC Drivers

    Type Description
    Type 1 JDBC-ODBC Bridge
    Type 2 Native API
    Type 3 Network Protocol
    Type 4 Thin Driver (most used)

    🔷 5. Steps to Connect to Database

    ✔ Step-by-step:

    1. Load Driver
    Class.forName("com.mysql.cj.jdbc.Driver");
    
    1. Establish Connection
    Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/mydb", "root", "password");
    
    1. Create Statement
    Statement stmt = con.createStatement();
    
    1. Execute Query
    ResultSet rs = stmt.executeQuery("SELECT * FROM student");
    
    1. Process Result
    while(rs.next()) {
        System.out.println(rs.getInt(1) + " " + rs.getString(2));
    }
    
    1. Close Connection
    con.close();
    

    🔷 6. Example Program

    import java.sql.*;
    
    public class JDBCExample {
        public static void main(String[] args) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
    
                Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/mydb", "root", "password");
    
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM student");
    
                while (rs.next()) {
                    System.out.println(rs.getInt("id") + " " + rs.getString("name"));
                }
    
                con.close();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    

    🔷 7. Important JDBC Interfaces

    Interface Purpose
    Connection Connect to database
    Statement Execute SQL
    PreparedStatement Precompiled SQL
    ResultSet Store result

    🔹 PreparedStatement Example

    PreparedStatement ps = con.prepareStatement(
        "INSERT INTO student VALUES (?, ?)");
    
    ps.setInt(1, 1);
    ps.setString(2, "Ali");
    
    ps.executeUpdate();
    

    👉 Advantages:

    • Faster execution
    • Prevents SQL injection

    🔷 8. Types of Statements

    Type Use
    Statement Simple SQL
    PreparedStatement Parameterized queries
    CallableStatement Stored procedures

    🔷 9. Exception Handling in JDBC

    • Uses try-catch blocks
    • Common exception: SQLException
    catch(SQLException e) {
        System.out.println(e.getMessage());
    }
    

    🔷 10. Transactions in JDBC

    • Used for multiple operations
    • Ensures data consistency
    con.setAutoCommit(false);
    
    // SQL operations
    
    con.commit();   // save changes
    con.rollback(); // undo changes
    

    🔷 11. Important Rules

    • Always close connection
    • Use PreparedStatement for security
    • Handle exceptions properly
    • Use transactions when needed

    🔷 12. Advantages of JDBC

    • Platform independent
    • Supports multiple databases
    • Easy database connectivity
    • Secure (with PreparedStatement)

    🔷 13. Diagram Description

    📊 JDBC Working Diagram:

    • Java Application
    • JDBC API
    • Driver Manager
    • Database

    Arrows showing flow of data and commands


    📝 Likely Exam Questions

    1. What is JDBC? Explain its architecture.
    2. Explain steps to connect Java with database.
    3. What are types of JDBC drivers?
    4. Difference between Statement and PreparedStatement.
    5. What is ResultSet?
    6. Write a JDBC program to fetch data.
    7. Explain transaction management in JDBC.
    8. What is SQLException?
    9. What is the role of DriverManager?
    10. Advantages of JDBC.

    📌 Quick Revision Summary

    • JDBC = Java Database Connectivity

    • Connects Java to database

    • Steps:

      1. Load driver
      2. Connect
      3. Create statement
      4. Execute query
      5. Process result
      6. Close connection
    • Use PreparedStatement for security

    • Handle exceptions properly


    ← Previous topic 4
    Applets and Swing
    Next topic 6 →
    Multithreading

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    519words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Computer Programming