Thursday, 3 April 2025

Write a JDBC Program to Insert the record into patient table (Use prepared statement)

 

import java.sql.*;

 

class InsertRecord1 {

    public static void main(String args[]) throws Exception {

               Class.forName("com.mysql.jdbc.Driver");

                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mca", "root", "");

                 String sql = "INSERT INTO class1 (cno, cname) VALUES (?, ?)";

        PreparedStatement pstmt = con.prepareStatement(sql);

 

             pstmt.setInt(1, 1);

        pstmt.setString(2, "CS");

        pstmt.executeUpdate(); //  first record

 

        pstmt.setInt(1, 2);

        pstmt.setString(2, "IT");

        pstmt.executeUpdate(); // second record

 

        pstmt.setInt(1, 3);

        pstmt.setString(2, "AI");

        pstmt.executeUpdate(); //  third record

         

 System.out.println("records inserted successfully ");

                pstmt.close();

        con.close();

    }

}