Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Advance Java procedure practical

The document contains a Java program that performs CRUD operations on a student database using JDBC and stored procedures. It includes methods for inserting, updating, deleting, and viewing student records, as well as the SQL queries for creating the corresponding stored procedures. Additionally, it provides examples of stored procedures that utilize IN, OUT, and INOUT parameters for various database operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Advance Java procedure practical

The document contains a Java program that performs CRUD operations on a student database using JDBC and stored procedures. It includes methods for inserting, updating, deleting, and viewing student records, as well as the SQL queries for creating the corresponding stored procedures. Additionally, it provides examples of stored procedures that utilize IN, OUT, and INOUT parameters for various database operations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

package procedure;

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

public class pro {


Scanner sc = new Scanner(System.in);

public void printTable(ResultSet rs) throws SQLException {


System.out.println("\
n--------------------------------------------------------------------------
------------------------------");
System.out.println("Id\tFirst Name\tLast Name\tContact\t\tBranch\t\
tAddress\t\tDOB");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t\
t" + rs.getString(3) + "\t\t" + rs.getString(4) + "\t" + rs.getString(5) +
"\t\t" + rs.getString(6) + "\t\t" + rs.getString(7));
}

System.out.println("-------------------------------------------------------
-------------------------------------------------");
}

public int updateCommon(CallableStatement update, String column) throws


SQLException {
int i = 0;
pro obj = new pro();
System.out.println("\nEnter " + column + " Value: ");
update.setString(1, obj.sc.next());
System.out.println("\nEnter Id: ");
update.setInt(2, obj.sc.nextInt());
i = update.executeUpdate();
return i;
}

public static void main(String[] args) throws Exception{


pro obj = new pro();
boolean flag = true;
int choice = 0;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3307/storeprocedure",
"root", "root");

CallableStatement insert = con.prepareCall("{CALL insert_student(?,


?, ?, ?, ?, ?, ?)}");

CallableStatement updateFname = con.prepareCall("{CALL


update_student_firstname(?, ?)}");
CallableStatement updateLname = con.prepareCall("{CALL
update_student_lastname(?, ?)}");
CallableStatement updateContact = con.prepareCall("{CALL
update_student_contact(?, ?)}");
CallableStatement updateBranch = con.prepareCall("{CALL
update_student_branch(?, ?)}");
CallableStatement updateAddress = con.prepareCall("{CALL
update_student_address(?, ?)}");
CallableStatement updateDOB = con.prepareCall("{CALL
update_student_dob(?, ?)}");

CallableStatement delete = con.prepareCall("{CALL


delete_student(?)}");
CallableStatement view = con.prepareCall("{CALL view_student(?)}");
CallableStatement viewAll = con.prepareCall("{CALL
view_all_students()}");

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

(1, 'Rahul', 'Gupta', '1234567890', 30, 'Computer Science', 'Ahmedabad','01-01-1999'),

(1, 'Priya', 'Sharma', '9876543210', 31, 'Electrical Engineering', 'Surat','02-02-1998'),

(3, 'Amit', 'Verma', '5555555555', 32, 'Mechanical Engineering', 'Vadodara','03-03-2000'),

(4, 'Sneha', 'Joshi', '1112223333', 33, 'Civil Engineering', 'Rajkot','04-04-1999'),

(5, 'Vikram', 'Reddy', '7778889999', 34, 'Computer Science', 'Bhavnagar','05-05-2001'),

(6, 'Deepika', 'Chopra', '4444444444', 35, 'Electrical Engineering', 'Jamnagar','06-06-1998'),

(7, 'Rahul', 'Mehta', '6666666666', 36, 'Mechanical Engineering', 'Gandhinagar','07-07-1999'),

(8, 'Pooja', 'Patel', '2223334444', 37, 'Civil Engineering', 'Junagadh','08-08-2000'),

(9, 'Rajesh', 'Kumar', '9999999999', 38,'Computer Science', 'Anand','09-09-1997'),

(10, 'Anjali', 'Singh', '8888888888', 39,'Electrical Engineering', 'Bharuch','10-10-2001'),

(11, 'Raju', 'Kumar', '7777799999', 40, 'Computer Science', 'Nadiad','09-09-1997'),

(12, 'Anju', 'Singh', '9999788888', 41, 'Electrical Engineering', 'Umreth','10-10-2001');

The provided code assumes the existence of the following stored procedures in the database:

1. `insert_student`: Inserts a new student record into the `student` table.

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

5. `update_student_branch`: Updates the branch of a student based on the given ID.

Query
CREATE PROCEDURE update_student_branch(IN branch VARCHAR(50), IN id
INT)
BEGIN
UPDATE student SET Branch = branch WHERE id = id;
END

6. `update_student_address`: Updates the address of a student based on the given ID.

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.

Query for Out Procedure


CREATE PROCEDURE get_student_count(OUT total_count INT)
BEGIN
SELECT COUNT(*) INTO total_count FROM student;
END;

```

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:

Query for In Out Procedure

CREATE PROCEDURE increase_student_score(INOUT student_id INT, IN


increment INT)
BEGIN
DECLARE current_score INT;

-- Get the current score for the student


SELECT score INTO current_score FROM students WHERE id =
student_id;

-- Increase the score by the given increment


SET current_score = current_score + increment;

-- Update the student's score in the database


UPDATE students SET score = current_score WHERE id = student_id;

-- Set the INOUT parameter to the new score


SET student_id = current_score;
END

```

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.

You might also like