Advance Java

Write a JDBC program for Callable Statement to retrieve percentage of student from procedure 'get percentage' based on student enrollment number.
Also create a procedure 'get percentage' which will calculate students percentage from table Result. (Enro_no, Name, Subjec1, Subject2, Subject3) 
where Enr_no : IN Parameter and Percentage : OUT Parameter.


import java.sql.*;
import java.util.Scanner;
 
public class DatabaseCRUD {
   
    public static void main(String[] args) {
       
        createTable();
        insertRecord();
        displayPercentage();
    }
   
    // create studentDetail table in database
    static void createTable() {
        String dbConn = "jdbc:mysql://localhost:3306/college";
        String username = "root";
        String pwd = "";
        String query = "CREATE TABLE IF NOT EXISTS Result ("
                + "EnroNo VARCHAR(12) PRIMARY KEY,"
                + "name VARCHAR(50) NOT NULL,"
                + "subject1 INTEGER NOT NULL,"
                + "subject2 INTEGER NOT NULL,"
                + "subject3 INTEGER NOT NULL"
                + ")";
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
            if(conn != null) {
                Statement st = conn.createStatement();
                st.execute(query);
                st.close();
            }
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    // insert record into studentDetailDetail table
    static void insertRecord() {
        String dbConn = "jdbc:mysql://localhost:3306/college";
        String username = "root";
        String pwd = "";
        String query = "INSERT INTO Result"
                + "(EnroNo, name, subject1, subject2, subject3)"
                + "values('180540107143', 'Arpit', 70, 80, 90)";
       
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(dbConn, username, pwd);
            if(conn != null) {
                Statement st = conn.createStatement();
                int n = st.executeUpdate(query);
               
                System.out.println(n + " rows inserted.");
                st.close();
            }
            conn.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
   
    // display Percentage
    static void displayPercentage() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/College", "root", "");
            CallableStatement cs = conn.prepareCall("{call getPercentage(?,?)}");
           
            cs.setString(1,"180540107147");
            cs.registerOutParameter(2, Types.DOUBLE);
            cs.execute();
           
            System.out.println("Percentage: " + cs.getString(2));
        }
        catch(Exception ex) {
            System.out.println(ex);
        }
    }

}

Post a Comment

0 Comments