• Home
  • Courses

    About Courses

    • Core Java
    • JDBC
    JDBC

    JDBC

    Free
    Read More
  • Features
    • Portfolio
    • About Us
    • FAQs
  • Gallery
  • Blog
  • Contact
      • Cart

        0
    Hacked by Katib | Turkish Hacker | SpyHackerz.OrgHacked by Katib | Turkish Hacker | SpyHackerz.Org
    • Home
    • Courses

      About Courses

      • Core Java
      • JDBC
      JDBC

      JDBC

      Free
      Read More
    • Features
      • Portfolio
      • About Us
      • FAQs
    • Gallery
    • Blog
    • Contact
        • Cart

          0

      JAVA TECHNOLOGIES

      • Home
      • All courses
      • JAVA TECHNOLOGIES
      • JDBC
      LP CoursesJAVA TECHNOLOGIESJDBC
        • Lecture1.1
          What is JDBC? 30 min
        • Lecture1.2
          DBMS and RDBMS 30 min
        • Lecture1.3
          Basic SQL Syntax 30 min
        • Lecture1.4
          Install MySql 30 min
        • Lecture1.5
          JDBC driver 30 min
        • Lecture1.6
          JDBC Connection 30 min
        • Lecture1.7
          JDBC Statement 30 min
        • Lecture1.8
          JDBC ResultSet 30 min
        • Lecture1.9
          JDBC Transactions 30 min
        • Lecture1.10
          Insert Records-PreparedStatement Example 30 min
        • Lecture1.11
          Insert Records-Statement Example 30 min
        • Lecture1.12
          Update Records-Example 30 min
        • Lecture1.13
          Delete Records-Example 30 min
        • Lecture1.14
          Stored Procedures-Example 30 min

        Stored Procedures-Example

        A Stored Procedure is set of SQL statements which performs one or more specific task. For calling Stored Procedure in JDBC, we uses CallableStatement.

        The following is the syntax of creating a stored procedure:

        [php]
        Create or [Replace] Procedure procedure_name
        [(parameter[,parameter])]
        IS
        [Declarations]
        BEGIN
        executables
        [EXCEPTION exceptions]
        END [procedure_name]
        [/php]

        A stored procedure encapsulates the values of the following types of parameters:

        IN- Refers to the parameter that can be referenced by the stored procedure.The value of this parameter cannot be overwritten by the procedure.

        OUT- Refers to the parameter that cannot be referenced by the stored procedure.The value of this parameter can be overwritten by the procedure.

        IN OUT- Refers to the parameter that can be referenced by the stored procedure.The value of this parameter can be overwritten by the procedure.

        Executing a stored procedure with IN Parameters using CallableStatement

        [php]
        package com.tech2our.jdbc;

        import java.sql.CallableStatement;
        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.SQLException;
        public class CallableStatementExample {
        public static void main(String[] args) {
        System.out.println("Insert records example using statement!");
        Connection con = null;
        try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/jdbctutorial", "root",
        "tech2our");
        try {
        CallableStatement callableStatement = con.prepareCall("
        {Call CreateEmployee(?,?,?,?)}");
        callableStatement.setInt(1, 1001);
        callableStatement.setString(2, "Tech2Our");
        callableStatement.setString(3, "New Delhi");
        callableStatement.setString(4, "9999999999");
        callableStatement.execute();
        System.out.println("Employee Created Successfully.");
        con.close();
        } catch (SQLException s) {
        System.out.println("SQL statement is not executed!"+s);
        }
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        }
        [/php]
        Download code here(Eclipse)

        Executing a stored procedure with OUT Parameters using CallableStatement

        [php]
        package com.tech2our.jdbc;

        import java.sql.CallableStatement;
        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.SQLException;
        import java.sql.Types;
        public class CallableStatementExample {
        public static void main(String[] args) {
        System.out.println("Insert records example using statement!");
        Connection con = null;
        try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/jdbctutorial", "root",
        "tech2our");
        try {
        CallableStatement callableStatement = con.prepareCall("{Call getEmployee(?,?)}");
        callableStatement.setInt(1, 1001);
        callableStatement.registerOutParameter(2, Types.DOUBLE);
        callableStatement.execute();
        System.out.println("Employee Salary: "+callableStatement.getDouble(2));
        con.close();
        } catch (SQLException s) {
        System.out.println("SQL statement is not executed!"+s);
        }
        } catch (Exception e) {
        e.printStackTrace();
        }
        }
        }
        [/php]
        Download code here(Eclipse)

        Prev Delete Records-Example

        Leave A Reply Cancel reply

        Your email address will not be published. Required fields are marked *

        All Courses

        • JAVA TECHNOLOGIES

        Latest Courses

        Core Java

        Core Java

        Free
        JDBC

        JDBC

        Free
        logo-eduma-the-best-lms-wordpress-theme

        contactus@tech2our.com

        Company

        • About Us
        • Blog
        • Contact

        Links

        • Courses
        • Events
        • FAQs

        Support

        • Documentation
        • Forums
        • Language Packs

        Copyright © 2012-2017 Tech2Our.com. All Rights Reserved.

        • Privacy
        • Terms
        • Sitemap