Write in
NodeJS
Observation
5. Develop a Java standalone application that connects with the database (Oracle
/MySQL) and perform the CRUD operation on the database tables.
Important SQL Commands
1. SELECT: Used to retrieve data from a database.
2. INSERT: Used to add new data to a database.
3. UPDATE: Used to modify existing data in a database.
4. DELETE: Used to remove data from a database.
5. CREATE TABLE: Used to create a new table in a database.
6. ALTER TABLE: Used to modify the structure of an existing table.
7. DROP TABLE: Used to delete an entire table from a database.
8. WHERE: Used to filter rows based on a specified condition.
9. ORDER BY: Used to sort the result set in ascending or descending order.
10. JOIN: Used to combine rows from two or more tables based on a related column
betweenthem.
Steps to Connect Java Application with Database
Step 1 – Import the Packages
Step 2 – Load the drivers using the forName() method
Step 3 – Register the drivers using DriverManager
Step 4 – Establish a connection using the Connection class object
Step 5 – Create a statement
Step 6 – Execute the query
Step 7 – Close the connections
1. [Link] -Java file containing the code that connects to a MySQL database
and performs CRUD (Create, Read, Update, Delete) operations on a table.
[Link]
[Link].*;
[Link];
public class JDBCExample
{
public static void main(String[] args) throws SQLException
{
//Register Driver and Establish Connection
Connection con =[Link]("jdbc:oracle:thin:@localhost:1521:xe",
"system", "anurag");
Statement s = [Link]();
Scanner sc = new Scanner([Link]);
while (true)
{
[Link]("Choose an option:");
[Link]("1. Create Table");
[Link]("2. Insert Data");
[Link]("3. Select/Read Data");
[Link]("4. Update Data");
[Link]("5. Delete Data");
[Link]("6. Exit");
int choice = [Link]();
switch (choice)
{
case 1: // Create Table
[Link]("CREATE TABLE STUDENT(sidNUMBER(10), sname
VARCHAR2(20), saddr VARCHAR2(20))");
[Link]("Table created successfully.");
break;
case 2: // Insert Data
[Link]("Inserting Data into student table:");
[Link]("Enter student id: ");
intsid = [Link]();
[Link]("Enter student name: ");
String sname = [Link]();
[Link]("Enter student address: ");
String saddr = [Link]();
// Insert values into the table
[Link]("INSERT INTO STUDENT (sid, sname, saddr) VALUES(" + sid + ", '"
+sname + "', '" + saddr + "')");
[Link]("Data inserted successfully into student table");
break;
case 3: // Select/Read Data
[Link]("Reading data from student table:");
ResultSetrs = [Link]("SELECT * FROM STUDENT ");
while ([Link]())
{
[Link]("Student ID: " + [Link]("sid") + ", Name: " + [Link]("sname")
+ ",Address: " + [Link]("saddr"));
}
[Link]();
break;
case 4: // Update Data
[Link]("Enter student id to update: ");
intupdateId = [Link]();
[Link]("Enter new student name: ");
String newName = [Link]();
[Link]("Enter new student address: ");
String newAddr = [Link]();
// Update values in the table
[Link]("UPDATE STUDENT SET sname = '" + newName + "', saddr = '" +
newAddr+ "' WHERE sid = " + updateId);
[Link]("Data updated successfully for student id " + updateId);
break;
case 5: // Delete Data
[Link]("Enter student id to delete: ");
intdeleteId = [Link]();
// Delete values from the table
[Link]("DELETE FROM STUDENT WHERE sid = " + deleteId);
[Link]("Data deleted successfully for student id " + deleteId);
break;
case 6: // Exit
[Link]("Exiting...");
[Link]();
[Link]();
[Link]();
return;
default:[Link]("Invalid option. Please try again.");
}
}
}
}