JDBC CONNECTION: STEPS: https://dev.mysql.com/downloads/connector/j/?utm_source=chatgpt.com Download Archive Zip file: Extract file: Add the .jar to Eclipse Go back to Eclipse In the left Package Explorer : Right-click your project → Build Path → Configure Build Path... Then: Libraries → Classpath → Add External JARs...—-> Select .jar file —> Apply and Close If still error shows: Delete the module-info.java on solution explorer Servlet Connection: Select Tomcat version 7 EX NO. 19 — JAVA WITH SQL CONNECTIVITY import java.sql.*; public class ConnCheck { public static void main(String[] args) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); System.out.println("Connection successful"); con.close(); } catch (Exception e) { System.out.println(e); } } } EX NO. 20 — DML COMMANDS import java.sql.*; public class JDBC_DML { public static void main(String[] args) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); Statement stmt = con.createStatement(); stmt.executeUpdate( "CREATE TABLE IF NOT EXISTS employee (" + "ID INT PRIMARY KEY AUTO_INCREMENT, " + "NAME VARCHAR(20) NOT NULL, " + "DEPARTMENT VARCHAR(20) NOT NULL)"); System.out.println("\n--- INSERT ---"); stmt.executeUpdate( "INSERT INTO employee(NAME, DEPARTMENT) VALUES('Hira', 'IT')"); System.out.println("Record inserted successfully!"); System.out.println("\n--- SELECT AFTER INSERT ---"); ResultSet rs = stmt.executeQuery("SELECT * FROM employee"); System.out.println("ID\tNAME\tDEPARTMENT"); while (rs.next()) { System.out.println( rs.getInt("ID") + "\t" + rs.getString("NAME") + "\t" + rs.getString("DEPARTMENT")); } System.out.println("\n--- UPDATE ---"); stmt.executeUpdate( "UPDATE employee SET DEPARTMENT='Finance' WHERE NAME='Hira'"); System.out.println("Record updated successfully!"); ResultSet rs1 = stmt.executeQuery("SELECT * FROM employee"); System.out.println("ID\tNAME\tDEPARTMENT"); while (rs1.next()) { System.out.println( rs1.getInt("ID") + "\t" + rs1.getString("NAME") + "\t" + rs1.getString("DEPARTMENT")); } System.out.println("\n--- DELETE ---"); stmt.executeUpdate( "DELETE FROM employee WHERE NAME='Hira'"); System.out.println("Record deleted successfully!"); ResultSet rs2 = stmt.executeQuery("SELECT * FROM employee"); System.out.println("ID\tNAME\tDEPARTMENT"); while (rs2.next()) { System.out.println( rs2.getInt("ID") + "\t" + rs2.getString("NAME") + "\t" + rs2.getString("DEPARTMENT")); } con.close(); } catch (Exception e) { System.out.println(e); } } } EX NO. 21 — CREATE STATEMENT import java.sql.*; public class CreateTable { public static void main(String[] args) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); Statement stmt = con.createStatement(); stmt.executeUpdate( "CREATE TABLE IF NOT EXISTS employee (" + "ID INT PRIMARY KEY AUTO_INCREMENT, " + "NAME VARCHAR(20) NOT NULL, " + "DEPARTMENT VARCHAR(20) NOT NULL)"); System.out.println("Table created successfully"); con.close(); } catch (Exception e) { System.out.println(e);}}} EX NO. 22 — COMMIT, ROLLBACK AND SAVEPOINT import java.sql.*; public class pg4 { public static void main(String[] args) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.executeUpdate( "INSERT INTO employee(NAME, DEPARTMENT) VALUES('HIRA', 'IT')"); Savepoint sp1 = con.setSavepoint("SP1"); stmt.executeUpdate( "INSERT INTO employee(NAME, DEPARTMENT) VALUES('ANU', 'Sales')"); Savepoint sp2 = con.setSavepoint("SP2"); con.rollback(sp1); con.commit(); System.out.println( "Transaction completed with rollback to SP1."); con.close(); } catch (Exception e) { System.out.println(e); } } } Core sequence setAutoCommit(false) ↓ INSERT HIRA ↓ SP1 ↓ INSERT ANU ↓ SP2 ↓ rollback(sp1) ↓ commit() So: HIRA stays. ANU is removed. EX NO. 23 — PREPARED STATEMENT import java.sql.*; public class pg5 { public static void main(String[] args) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); PreparedStatement ps = con.prepareStatement( "INSERT INTO students(id, name, age) VALUES(?, ?, ?)"); ps.setInt(1, 7); ps.setString(2, "HIRA"); ps.setInt(3, 20); int i = ps.executeUpdate(); System.out.println( i + " record inserted successfully."); con.close(); } catch (Exception e) { System.out.println(e); } } } EX NO. 24 — INSERT RECORD BY GETTING FROM USER import java.sql.*; import java.util.Scanner; public class pg6 { public static void main(String[] args) { try { Scanner sc = new Scanner(System.in); Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); PreparedStatement ps = con.prepareStatement( "INSERT INTO employee(NAME, DEPARTMENT) VALUES(?, ?)"); System.out.print("Enter Employee Name: "); String name = sc.nextLine(); System.out.print("Enter Department: "); String dept = sc.nextLine(); ps.setString(1, name); ps.setString(2, dept); int i = ps.executeUpdate(); System.out.println( i + " record inserted successfully."); con.close(); sc.close(); } catch (Exception e) { System.out.println(e); } } } EX NO. 25 — DELETE RECORD BASED ON USER INPUT import java.sql.*; import java.util.Scanner; public class pg7 { public static void main(String[] args) { try { Scanner sc = new Scanner(System.in); Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); PreparedStatement ps = con.prepareStatement( "DELETE FROM employee WHERE ID = ?"); System.out.print("Enter Employee ID to delete: "); int id = sc.nextInt(); ps.setInt(1, id); int i = ps.executeUpdate(); if (i > 0) System.out.println("Record deleted successfully!"); else System.out.println("No record found with that ID."); con.close(); sc.close(); } catch (Exception e) { System.out.println(e); } } } EX NO. 26 — BATCH COMMANDS import java.sql.*; public class pg8 { public static void main(String[] args) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/nandhinisri_db", "root", "root"); con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.addBatch( "INSERT INTO employee(NAME, DEPARTMENT) VALUES('Kiran', 'IT')"); stmt.addBatch( "INSERT INTO employee(NAME, DEPARTMENT) VALUES('Divya', 'Finance')"); stmt.addBatch( "UPDATE employee SET DEPARTMENT='Admin' WHERE NAME='Kiran'"); int[] count = stmt.executeBatch(); con.commit(); System.out.println( "Batch executed successfully! " + count.length + " statements processed."); con.close(); } catch (Exception e) { System.out.println(e); } } } EX NO. 27 — SCROLLABLE RESULT SET import java.sql.*; public class Record { public static void main(String[] args) { try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student", "root", "root"); Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery( "SELECT * FROM employee"); rs.afterLast(); System.out.println( "Employee Records in Reverse Order:"); while (rs.previous()) { System.out.println( rs.getInt("ID") + "\t" + rs.getString("NAME") + "\t" + rs.getString("DEPARTMENT")); } con.close(); } catch (Exception e) { System.out.println(e); } } }