0% found this document useful (0 votes)
7 views

5. Develop a java stand-alone application that connects with the database (Oracle MySQL) and perform t

The document provides Java source code for a stand-alone application that performs CRUD operations on a MySQL database for a 'student' table. It includes four classes: InsertData for inserting records, DisplayData for retrieving records, UpdateData for modifying records, and DeleteData for removing records. Each class handles database connections and operations using JDBC, with error handling for SQL exceptions and driver loading issues.

Uploaded by

Kore Ramesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

5. Develop a java stand-alone application that connects with the database (Oracle MySQL) and perform t

The document provides Java source code for a stand-alone application that performs CRUD operations on a MySQL database for a 'student' table. It includes four classes: InsertData for inserting records, DisplayData for retrieving records, UpdateData for modifying records, and DeleteData for removing records. Each class handles database connections and operations using JDBC, with error handling for SQL exceptions and driver loading issues.

Uploaded by

Kore Ramesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

5.

Develop a java stand-alone application that connects with the database (Oracle / MySQL)
and perform the CRUD operation on the database tables.

Java Source Code Documentation

InsertData.java
### Description:
This Java file contains the implementation for database operations.

import java.sql.*;
import java.util.Scanner;

public class InsertData {


public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");

try (Connection con =


DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "123456");
Statement s = con.createStatement();
Scanner sc = new Scanner(System.in)) {

System.out.println("Inserting Data into student table:");


System.out.println("________________________________________");

System.out.print("Enter student id: ");


int sid = sc.nextInt();
System.out.print("Enter student name: ");
String sname = sc.next();
System.out.print("Enter student address: ");
String saddr = sc.next();

String insertQuery = "INSERT INTO student VALUES(" + sid + ",'" +


sname + "','" + saddr + "')";
s.executeUpdate(insertQuery);

System.out.println("Data inserted successfully into student


table");
}
} catch (SQLException err) {
System.out.println("ERROR: " + err);
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found.");
e.printStackTrace();
}
}
}

PS C:\Users\rames\OneDrive\1.PEC\2-2\Lab Programs\5> javac -cp ".;mysql-


connector-j-9.2.0.jar" InsertData.java
PS C:\Users\rames\OneDrive\1.PEC\2-2\Lab Programs\5> java -cp ".;mysql-connector-
j-9.2.0.jar" InsertData
Inserting Data into student table:
________________________________________
Enter student id: 2
Enter student name: Ramesh
Enter student address: Hyderabad
Data inserted successfully into student table

DisplayData.java
### Description:
This Java file contains the implementation for database operations.

import java.sql.*;

public class DisplayData {


public static void main(String[] args) {
try {
// Load the MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");

try (Connection con =


DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "123456");
Statement s = con.createStatement()) {

ResultSet rs = s.executeQuery("SELECT * FROM student");


if (rs != null) {
System.out.println("SID \t STU_NAME \t ADDRESS");
System.out.println("________________________________________");

while (rs.next()) {
System.out.println(rs.getString("s_id") + " \t " +
rs.getString("s_name") + " \t " + rs.getString("s_address"));
System.out.println("________________________________________");
}
}

} catch (SQLException err) {


System.out.println("ERROR: " + err);
}
} catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found. Include it in your library
path.");
e.printStackTrace();
}
}
}

PS C:\Users\rames\OneDrive\1.PEC\2-2\Lab Programs\5> javac -cp ".;mysql-


connector-j-9.2.0.jar" DisplayData.java
PS C:\Users\rames\OneDrive\1.PEC\2-2\Lab Programs\5> java -cp ".;mysql-connector-
j-9.2.0.jar" DisplayData
SID STU_NAME ADDRESS
________________________________________
1 rama Hyd
________________________________________
2 Ramesh Hyderabad
________________________________________

updateData.java
### Description:
This Java file contains the implementation for database operations.

import java.sql.*;
import java.util.Scanner;

public class UpdateData {


public static void main(String[] args) {
try (Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "123456");
Statement s = con.createStatement()) { // Changed 'Statements' to
'Statement s'

Scanner sc = new Scanner(System.in);


System.out.println("Update Data in student table:");
System.out.println("________________________________________");

System.out.print("Enter student id: ");


int sid = sc.nextInt();
System.out.print("Enter student name: ");
String sname = sc.next();
System.out.print("Enter student address: ");
String saddr = sc.next();
String updateQuery = "UPDATE student SET s_name='" + sname + "',
s_address='" + saddr + "' WHERE s_id=" + sid;
s.executeUpdate(updateQuery);

System.out.println("Data updated successfully");

} catch (SQLException err) {


System.out.println("ERROR: " + err);
}
}
}

PS C:\Users\rames\OneDrive\1.PEC\2-2\Lab Programs\5> javac -cp ".;mysql-


connector-j-9.2.0.jar" UpdateData.java
PS C:\Users\rames\OneDrive\1.PEC\2-2\Lab Programs\5> java -cp ".;mysql-connector-
j-9.2.0.jar" UpdateData
Update Data in student table:
________________________________________
Enter student id: 1
Enter student name: Ramesh Kore
Enter student address: Data updated successfully

deleteData.java
### Description:
This Java file contains the implementation for database operations.

import java.sql.*;
import java.util.Scanner;

public class DeleteData {


public static void main(String[] args) {
try (Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "123456");
Statement s = con.createStatement()) { // Changed 'Statements' to
'Statement s'

Scanner sc = new Scanner(System.in);


System.out.println("Delete Data from student table:");
System.out.println("________________________________________");

System.out.print("Enter student id: ");


int sid = sc.nextInt();
String deleteQuery = "DELETE FROM student WHERE s_id=" + sid;
s.executeUpdate(deleteQuery);

System.out.println("Data deleted successfully");

} catch (SQLException err) {


System.out.println("ERROR: " + err);
}
}
}

You might also like