ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Computer Programming
    COMP3114
    Progress0 / 12 topics
    Topics
    1. Java API: Abstract classes and Interfaces2. Packages and Exception handling3. Advanced issues of GUI and event handling4. Applets and Swing5. Network Programming Concepts: JDBC6. Multithreading7. Building Client/Server and implementing protocols8. RMI (Remote Method Invocation)9. Java Secure Socket Extension and Secure Sockets Layer (SSL)10. SSL Socket and SSL Server Socket classes11. Client and Server Authentication: HTTPS12. Developing TCP/IP client and server with telnet
    COMP3114›Network Programming Concepts: JDBC
    Advanced Computer ProgrammingTopic 5 of 12

    Network Programming Concepts: JDBC

    3 minread
    519words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count519
      Code examples0
      DifficultyBeginner