Sr. No. | Date | Program List | Pg. No | Sign |
---|---|---|---|---|
1 | Program on JDBC using statements. | |||
2 | 01-02-2025 | Program to demonstrate Generic and HTTP Servlet response and request objects. | ||
3 | 15-02-2025 | Program to demonstrate Http session using cookies, URL rewritten, Hidden Fields. | ||
4 | Program to demonstrate online web applications(Online Quiz ) | |||
5 | 22-02-2025 | Program to demonstrate Page directives with JSP | ||
6 | Program to demonstrate Http session using cookies. | |||
7 | Program to demonstrate Error Page in JSP. | |||
8 | 22-02-2025 | Program to demonstrate JSP Actions, JSP with beans | ||
9 | Program to demonstrate to custom tags, JSTL tags | |||
10 | Program to demonstrate simple XML tags | |||
11 | Program to demonstrate DTD, XML Parser, Validator. | |||
12 | 29-03-2025 | Program to Write a Hibernate Application, Configuring Hibernate for our application, | ||
13 | 05-04-2025 | Program to fetch data from the database using Hibernate, Annotations, Hibernate | ||
14 | Building first Spring Application | |||
15 | Simple web application using Spring |
Connection Driver and URL.
RDBMS | JDBC Driver Name | URL Format |
---|---|---|
MySQL | com.mysql.jdbc.Driver |
jdbc:mysql://hostname/databaseName/ |
ORACLE | oracle.jdbc.driver.OracleDriver |
jdbc:oracle:thin:@hostname:portNumber:databaseName |
DB2 | COM.ibm.db2.jdbc.net.DB2Driver |
jdbc:db2:hostname:portNumber/databaseName |
Sybase | com.sybase.jdbc.SybDriver |
jdbc:sybase:Tds:hostname:portNumber/databaseName |
Postgres | org.postgresql.Driver |
jdbc:postgresql://hostname:port/databaseName |
Access | sun.jdbc.odbc.JdbcOdbcDriver |
jdbc:odbc:databaseName |
Note: Both the “:” and “@” are mandatory.
Write a java program to connect with the MySQL database & insert n number of records in ___ table using prepareStatement
.
Code:
import java.sql.*;
import java.util.Scanner;
public class InsertStudentRecords {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mcadb202426";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) {
Connection connection = null;
String query = "INSERT INTO students (rollno, email, course, city, reg_time) VALUES (?, ?, ?, ?, ?)";
try {
connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
System.out.println("Connected to the database successfully!");
PreparedStatement pst = connection.prepareStatement(query);
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of records to insert: ");
int n = sc.nextInt();
sc.nextLine(); // Consume the leftover newline
for (int i = 1; i <= n; i++) {
System.out.println("Enter details for student " + i + ":");
System.out.print("Roll Number: ");
String rollno = sc.nextLine();
System.out.print("Email: ");
String email = sc.nextLine();
System.out.print("Course: ");
String course = sc.nextLine();
System.out.print("City: ");
String city = sc.nextLine();
System.out.print("Registration Time (YYYY-MM-DD HH:MM:SS): ");
String regTime = sc.nextLine();
pst.setString(1, rollno);
pst.setString(2, email);
pst.setString(3, course);
pst.setString(4, city);
pst.setString(5, regTime);
pst.executeUpdate();
System.out.println("Record for student " + i + " inserted successfully.");
}
sc.close();
System.out.println("All records inserted.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:
>>> java -cp "Class-Files;D:\\Documents\\MCA Course\\MCA-Coursework\\SEM-1\\Core-Java\\Drivers\\mysql-connector-j-9.1.0\\mysql-connector-j-9.1.0.jar" InsertStudentRecords
Connected to the database successfully!
Enter the number of records to insert: 2
Enter details for student 1:
Roll Number: 192
Email: [email protected]
Course: MBA
City: Mumbai
Registration Time (YYYY-MM-DD HH:MM:SS): 2024-12-21 02:41:26
Record for student 1 inserted successfully.
Enter details for student 2:
Roll Number: 193
Email: [email protected]
Course: MCA
City: Mumbai
Registration Time (YYYY-MM-DD HH:MM:SS): 2024-12-21 02:41:26
Record for student 2 inserted successfully.
All records inserted.
Write a java program to connect with the database & update records using MySQL database.
Code:
import java.sql.*;
import java.util.Scanner;
public class UpdateStudentRecords {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mcadb202426";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) {
Connection connection = null;
String query = "UPDATE students SET rollno = ?, email = ?, course = ?, city = ?, reg_time = ? WHERE rollno = ?";
try {
connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
System.out.println("Connected to the database successfully!");
PreparedStatement pst = connection.prepareStatement(query);
Scanner sc = new Scanner(System.in);
System.out.print("Enter the roll number of the student to update: ");
String rollnoToUpdate = sc.nextLine();
System.out.print("Enter new Roll Number: ");
String newRollno = sc.nextLine();
System.out.print("Enter new Email: ");
String newEmail = sc.nextLine();
System.out.print("Enter new Course: ");
String newCourse = sc.nextLine();
System.out.print("Enter new City: ");
String newCity = sc.nextLine();
System.out.print("Enter new Registration Time (YYYY-MM-DD HH:MM:SS): ");
String newRegTime = sc.nextLine();
pst.setString(1, newRollno);
pst.setString(2, newEmail);
pst.setString(3, newCourse);
pst.setString(4, newCity);
pst.setString(5, newRegTime);
pst.setString(6, rollnoToUpdate);
int rowsAffected = pst.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Record with roll number " + rollnoToUpdate + " updated successfully.");
} else {
System.out.println("No record found with roll number " + rollnoToUpdate + ".");
}
sc.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:
>>> java -cp "Class-Files;D:\\Documents\\MCA Course\\MCA-Coursework\\SEM-1\\Core-Java\\Drivers\\mysql-connector-j-9.1.0\\mysql-connector-j-9.1.0.jar" UpdateStudentRecords
Connected to the database successfully!
Enter the roll number of the student to update: 1
Enter new Roll Number: 101
Enter new Email: [email protected]
Enter new Course: MCA
Enter new City: Delhi
Enter new Registration Time (YYYY-MM-DD HH:MM:SS): 2024-06-05 01:00:00
Record with roll number 1 updated successfully.
Write a program to connect with the database & delete records using MySQL database.
Code:
import java.sql.*;
import java.util.Scanner;
public class DeleteStudentRecord {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mcadb202426";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) {
Connection connection = null;
String query = "DELETE FROM students WHERE rollno = ?";
try {
connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
System.out.println("Connected to the database successfully!");
PreparedStatement pst = connection.prepareStatement(query);
Scanner sc = new Scanner(System.in);
System.out.print("Enter the roll number of the student to delete: ");
String rollnoToDelete = sc.nextLine();
pst.setString(1, rollnoToDelete);
int rowsAffected = pst.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Record with roll number " + rollnoToDelete + " deleted successfully.");
} else {
System.out.println("No record found with roll number " + rollnoToDelete + ".");
}
sc.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output:
>>> java -cp "Class-Files;D:\\Documents\\MCA Course\\MCA-Coursework\\SEM-1\\Core-Java\\Drivers\\mysql-connector-j-9.1.0\\mysql-connector-j-9.1.0.jar" DeleteStudentRecord
Connected to the database successfully!
Enter the roll number of the student to delete: 101
Record with roll number 101 deleted successfully.
Write a java program to connect with JDBC and retrieve records from any one table.
Code:
import java.sql.*;
public class RetrieveStudentRecords {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mcadb202426";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) {
Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
String query = "SELECT * FROM students";
try {
connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
System.out.println("Connected to the database successfully!");
stmt = connection.createStatement();
rs = stmt.executeQuery(query);
if (!rs.next()) {
System.out.println("No records found in the students table.");
return;
}
System.out.printf("%-15s %-30s %-20s %-15s %-20s%n", "RollNo", "Email", "Course", "City", "Reg_Time");
System.out.println(
"---------------------------------------------------------------------------------------------");
do {
String rollno = rs.getString("rollno");
String email = rs.getString("email");
String course = rs.getString("course");
String city = rs.getString("city");
String regTime = rs.getString("reg_time");
System.out.printf("%-15s %-30s %-20s %-15s %-20s%n", rollno, email, course, city, regTime);
} while (rs.next());
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (connection != null) {
connection.close();
System.out.println("Connection closed.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Output:
>>> java -cp "Class-Files;D:\\Documents\\MCA Course\\MCA-Coursework\\SEM-1\\Core-Java\\Drivers\\mysql-connector-j-9.1.0\\mysql-connector-j-9.1.0.jar" RetrieveStudentRecords
Connected to the database successfully!
RollNo Email Course City Reg_Time
---------------------------------------------------------------------------------------------
2 [email protected] MCA Mumbai 2024-11-20 06:41:00
3 [email protected] MET Mumbai 2024-11-20 06:41:00
4 [email protected] M.Arts THANE 2024-11-20 06:42:00
5 [email protected] B.Tech Pune 2024-11-22 09:00:00
6 [email protected] MBA Delhi 2024-11-22 09:10:00
7 [email protected] MCA Bangalore 2024-11-22 09:20:00
8 [email protected] M.Sc Chennai 2024-11-22 09:30:00
9 [email protected] BBA Hyderabad 2024-11-22 09:40:00
10 [email protected] PhD Ahmedabad 2024-11-22 09:50:00
11 [email protected] B.Com Kolkata 2024-11-22 10:00:00
12 [email protected] M.Tech Lucknow 2024-11-22 10:10:00
13 [email protected] B.Sc Jaipur 2024-11-22 10:20:00
14 [email protected] B.A Surat 2024-11-22 10:30:00
191 [email protected] MCA Mumbai' 2024-11-01 12:11:01
192 [email protected] MBA Mumbai 2024-12-21 02:41:26
193 [email protected] MCA Mumbai 2024-12-21 02:41:26
Connection closed.