AJP Lab Exercise-1


1. Write a JDBC application which will interact with Database and perform the following task.

     a. Create Student Table with Roll No, Name, and Address field and insert few records. 
   b. Using Statement Object display the content of Record. 
   c. Using Statement Object Insert Two Record. 
   d. Using Statement Object Update One Record. 
   e. Using Statement Object Delete One Record. 
   f. Using Statement Object display the content of Record.



import java.sql.*;

class StudentJDBCApp {
    public static void main(String[] args) {
        Connection con = null;
        Statement stmt = null;

        try {
            // 1. Load Driver (recommended for older versions)
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. Establish Connection
            con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/studentdb?autoReconnect=true&useSSL=false", 
                "root", 
                "root"
            );

            // 3. Create Statement
            stmt = con.createStatement();

            // a. Create Table (corrected to Student)
            String createTableSQL = "CREATE TABLE IF NOT EXISTS Student (" +
                    "RollNo INT PRIMARY KEY," +
                    "Name VARCHAR(100)," +
                    "Address VARCHAR(100))";
            stmt.executeUpdate(createTableSQL);
            System.out.println("Table Created.");

            // b. Insert few initial records
            stmt.executeUpdate("INSERT INTO Student VALUES (101, 'Ravi', 'Hyderabad')");
            stmt.executeUpdate("INSERT INTO Student VALUES (102, 'Sita', 'Delhi')");
            System.out.println("Initial records inserted.");

            // c. Display all records
            System.out.println("\nAll Records:");
            ResultSet rs = stmt.executeQuery("SELECT * FROM Student");
            while (rs.next()) {
                System.out.println(
                    rs.getInt("RollNo") + " - " +
                    rs.getString("Name") + " - " +
                    rs.getString("Address")
                );
            }

            // d. Insert two more records
            stmt.executeUpdate("INSERT INTO Student VALUES (103, 'Rahul', 'Chennai')");
            stmt.executeUpdate("INSERT INTO Student VALUES (104, 'Neha', 'Mumbai')");
            System.out.println("\nTwo more records inserted.");

            // e. Update one record
            stmt.executeUpdate("UPDATE Student SET Address='Bangalore' WHERE RollNo=102");
            System.out.println("Record with RollNo=102 updated.");

            // f. Delete one record
            stmt.executeUpdate("DELETE FROM Student WHERE RollNo=101");
            System.out.println("Record with RollNo=101 deleted.");

            // g. Display all records again
            System.out.println("\nFinal Records:");
            rs = stmt.executeQuery("SELECT * FROM Student");
            while (rs.next()) {
                System.out.println(
                    rs.getInt("RollNo") + " - " +
                    rs.getString("Name") + " - " +
                    rs.getString("Address")
                );
            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 4. Close resources
            try {
                if (stmt != null) stmt.close();
                if (con != null) con.close();
            } catch (Exception e) {
                System.err.println("Error closing resources: " + e.getMessage());
            }
        }
    }
}