0% found this document useful (0 votes)
53 views3 pages

Practical-3.3: Program To Insert Values in A Table at Run Time Using JDBC

The code shows how to insert values into a database table using JDBC in 3 steps: 1) It connects to a MySQL database called STUDENTS using the JDBC driver, username, and password. 2) It creates a Statement object to execute SQL statements and inserts 4 records into the Registration table with insert queries. 3) It closes the Statement and Connection in the finally block.

Uploaded by

Saket Nisan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views3 pages

Practical-3.3: Program To Insert Values in A Table at Run Time Using JDBC

The code shows how to insert values into a database table using JDBC in 3 steps: 1) It connects to a MySQL database called STUDENTS using the JDBC driver, username, and password. 2) It creates a Statement object to execute SQL statements and inserts 4 records into the Registration table with insert queries. 3) It closes the Statement and Connection in the finally block.

Uploaded by

Saket Nisan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

NAME MOHD ALI

UID 20BCS2041
SECTION 605-A
DATE 06/11/2021

Practical-3.3
Program to insert values in a table at run time using JDBC.

● CODE:-

import java.sql.*;

public class JDBCExample {


// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";

// Database credentials
static final String USER = "username";
static final String PASS = "password";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;

try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
System.out.println("Inserting records into the table...");
stmt = conn.createStatement();
String sql = "INSERT INTO Registration " +
"VALUES (100, 'Zara', 'Ali', 18)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration " +
"VALUES (101, 'Mahnaz', 'Fatma', 25)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration " +
"VALUES (102, 'Zaid', 'Khan', 30)";
stmt.executeUpdate(sql);
sql = "INSERT INTO Registration " +
"VALUES(103, 'Sumit', 'Mittal', 28)";
stmt.executeUpdate(sql);
System.out.println("Inserted records into the table...");

catch(SQLException se){
se.printStackTrace();
}

catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}

finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}

● OUTPUT:-

You might also like