Advance Java procedure practical
Advance Java procedure practical
import java.sql.*;
import java.util.Scanner;
System.out.println("-------------------------------------------------------
-------------------------------------------------");
}
while (flag) {
System.out.println("\nEnter 1 For Insert");
System.out.println("Enter 2 For Update");
System.out.println("Enter 3 For Delete");
System.out.println("Enter 4 For View");
System.out.println("Enter 5 For View All");
System.out.println("Enter 6 For Exit");
System.out.println("\nEnter Your Choice: ");
if (obj.sc.hasNextInt()) {
choice = obj.sc.nextInt();
}
switch (choice) {
case 1:
System.out.println("Enter Student Id: ");
insert.setInt(1, obj.sc.nextInt());
System.out.println("Enter First Name: ");
insert.setString(2, obj.sc.next());
System.out.println("Enter Last Name: ");
insert.setString(3, obj.sc.next());
System.out.println("Enter Contact No: ");
insert.setString(4, obj.sc.next());
System.out.println("Enter Branch: ");
insert.setString(5, obj.sc.next());
System.out.println("Enter Address: ");
insert.setString(6, obj.sc.next());
System.out.println("Enter DOB(dd-mm-yyyy): ");
insert.setString(7, obj.sc.next());
int i = insert.executeUpdate();
System.out.println("\n" + i + " Row Inserted");
break;
case 2:
int updateCol = 0;
String column = "";
do {
System.out.println("-------Which Field You Want To
Update-------");
System.out.println("\nEnter 1 For First Name");
System.out.println("Enter 2 For Last Name");
System.out.println("Enter 3 For Contact No");
System.out.println("Enter 4 For Branch");
System.out.println("Enter 5 For Address");
System.out.println("Enter 6 For DOB");
System.out.println("Enter Your Choice: ");
updateCol = obj.sc.nextInt();
if (updateCol > 0 && updateCol <= 6)
break;
} while (true);
int up = 0;
switch (updateCol) {
case 1:
column = "First_name";
up = obj.updateCommon(updateFname, column);
break;
case 2:
column = "Last_name";
up = obj.updateCommon(updateLname, column);
break;
case 3:
column = "Contact_no";
up = obj.updateCommon(updateContact, column);
break;
case 4:
column = "Branch";
up = obj.updateCommon(updateBranch, column);
break;
case 5:
column = "Address";
up = obj.updateCommon(updateAddress, column);
break;
case 6:
column = "DOB";
up = obj.updateCommon(updateDOB, column);
break;
default:
System.out.println("Enter Valid Choice -_-");
break;
}
System.out.println(up + " Row Updated");
break;
case 3:
System.out.println("Enter Id: ");
delete.setInt(1, obj.sc.nextInt());
int del = delete.executeUpdate();
System.out.println(del + " Row Deleted");
break;
case 4:
System.out.println("Enter Id: ");
view.setInt(1, obj.sc.nextInt());
ResultSet rs = view.executeQuery();
obj.printTable(rs);
rs.close();
break;
case 5:
System.out.println();
ResultSet r = viewAll.executeQuery();
obj.printTable(r);
r.close();
break;
case 6:
flag = false;
break;
default:
System.out.println("Enter Valid Choice -_-");
break;
}
}
}
}
INSERT INTO book (id, First_name, Last_name, Contact_no, Score Branch, Address, DOB) VALUES
The provided code assumes the existence of the following stored procedures in the database:
Query
CREATE PROCEDURE insert_student(IN id INT, IN fname VARCHAR(50), IN
lname VARCHAR(50), IN contact VARCHAR(20), IN branch VARCHAR(50), IN
address VARCHAR(100), IN dob VARCHAR(10))
BEGIN
INSERT INTO student VALUES (id, fname, lname, contact, branch,
address, dob);
END
2. `update_student_firstname`: Updates the first name of a student based on the given ID.
Query
CREATE PROCEDURE update_student_firstname(IN fname VARCHAR(50), IN
id INT)
BEGIN
UPDATE student SET First_name = fname WHERE id = id;
END
3. `update_student_lastname`: Updates the last name of a student based on the given ID.
Query
CREATE PROCEDURE update_student_lastname(IN lname VARCHAR(50), IN id
INT)
BEGIN
UPDATE student SET Last_name = lname WHERE id = id;
END
4. `update_student_contact`: Updates the contact number of a student based on the given ID.
Query
CREATE PROCEDURE update_student_contact(IN contact VARCHAR(20), IN
id INT)
BEGIN
UPDATE student SET Contact_no = contact WHERE id = id;
END
Query
CREATE PROCEDURE update_student_branch(IN branch VARCHAR(50), IN id
INT)
BEGIN
UPDATE student SET Branch = branch WHERE id = id;
END
Query
CREATE PROCEDURE update_student_address(IN address VARCHAR(100), IN
id INT)
BEGIN
UPDATE student SET Address = address WHERE id = id;
END
7. `update_student_dob`: Updates the date of birth of a student based on the given ID.
Query
CREATE PROCEDURE update_student_dob(IN dob VARCHAR(10), IN id INT)
BEGIN
UPDATE student SET DOB = dob WHERE id = id;
END
8. `delete_student`: Deletes a student record from the `student` table based on the given ID.
Query
CREATE PROCEDURE delete_student(IN id INT)
BEGIN
DELETE FROM student WHERE id = id;
END
9. `view_student`: Retrieves a specific student record from the `student` table based on the
given ID.
Query
CREATE PROCEDURE view_student(IN id INT)
BEGIN
SELECT * FROM student WHERE id = id;
END
10. `view_all_students`: Retrieves all student records from the `student` table.
Query
CREATE PROCEDURE view_all_students()
BEGIN
SELECT * FROM student;
END
You need to execute these SQL statements in your MySQL database to create the stored procedures.
Once the stored procedures are created, you can run the Java program to perform CRUD operations
on the `student` table using callable statements.
```
In this stored procedure, the `get_student_count` procedure is created with an OUT parameter called
`total_count` of type INT. Inside the procedure, a SELECT statement is used to retrieve the count of
records from the `student` table, and the result is assigned to the `total_count` OUT parameter using
the `INTO` clause.
You can execute this SQL query in your MySQL database to create the `get_student_count` stored
procedure. After creating the stored procedure, you can use it in your Java program to retrieve the
total count of students using the OUT parameter.
Certainly! Here's an example of a stored procedure query that uses an INOUT parameter:
```
This stored procedure takes two parameters: `student_id` (INOUT) and `increment` (IN). It retrieves
the current score for the student specified by `student_id`, increases the score by the `increment`
value, updates the student's score in the database, and then sets the `student_id` parameter to the
new score. This way, when the stored procedure is executed, the `student_id` parameter will contain
the updated score value.