Tue. Mar 9th, 2021

Requirement: I have a button with the name “Execute SQL From Managed Bean“. On click of this button, it should execute an SQL query for the Departments table and should display the Department Id and the Department Name in the console. The SQL Query will get executed in the AppModuleImpl class

Solution: For the solution of the above requirement follow the steps as shown below:

Step 1: Create an Oracle ADF Fusion Web Application. Create an Application Module.

Create an AppModuIeImpl class.

Thus, the complete code inside AppModuIeImpl class is shown below.

package model;

import java.math.BigDecimal;

import java.sql.ResultSet;

import java.sql.SQLException;

import model.common.AppModule;

import oracle.jbo.JboException;
import oracle.jbo.server.ApplicationModuleImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Tue Feb 16 15:27:05 IST 2021
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class AppModuleImpl extends ApplicationModuleImpl implements AppModule {
    /**
     * This is the default constructor (do not remove).
     */
    public AppModuleImpl() {
    }

    public void executeSQLQueryinAMImpl(String query) {
        ResultSet rs;
        try {
            rs = getDBTransaction().createStatement(0).executeQuery(query);
            while (rs.next()) {
                System.out.println("Department Id is : " + ((BigDecimal) rs.getObject(1)).intValue() +
                                   " and Department Name is : " + rs.getString(2));
            }
            rs.close();
        } catch (SQLException e) {
            throw new JboException(e);
        }
    }
}

Step 2: Expose the executeSQLQueryinAMImpl method as client interface

Step 3: Create a demo.jspx page. Drag and drop and af:button and set text=”Execute SQL Inside AMImpl” and actionListener=”#{pageFlowScope.MyBean.executeSQLFromAMImplAL}

Thus, the complete code for demo.jspx page is shown below:

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="demo.jspx" id="d1">
            <af:form id="f1">
                <af:button text="Execute SQL Inside AMImpl" id="b1"
                           actionListener="#{pageFlowScope.MyBean.executeSQLFromAMImplAL}"/>
            </af:form>
        </af:document>
    </f:view>
</jsp:root>

And, the complete code of MyBean.java class is shown below:

package susantotech.com;

import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;

import oracle.adf.model.binding.DCBindingContainer;

import oracle.binding.OperationBinding;

public class MyBean {
    public MyBean() {
    }


    public void executeSQLFromAMImplAL(ActionEvent actionEvent) {
        DCBindingContainer bindings = getBindings();
        OperationBinding method = (OperationBinding) bindings.get("executeSQLQueryinAMImpl");
        method.getParamsMap().put("query", "SELECT * FROM departments");
        method.execute();
    }

    public DCBindingContainer getBindings() {
        return (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
    }
}

Step 4: Create the demoPageDef.xml file for the demo.jspx page. Add the executeSQLQueryinAMImpl in the binding.

Save all and run the application. Thus, the ran application is shown below:

Click on the Execute SQL Inside AMImpl” button and check the output in the JDeveloper console.

Hence, the solution to our requirement.

If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.

Thanks & Regards,
Susanto Paul

 122 total views,  1 views today

By Susanto Paul

Susanto is an Oracle ACE, a qualified MCA, MBA, and a highly-skilled Senior Oracle Specialist. He is an enthusiastic Blogger and YouTuber who helps learners to solve their complex problems more efficiently. He has 9+ years of experience in multiple technologies like AWS, Oracle ADF, Oracle APEX, Oracle JET, Oracle VBCS, Oracle IDCS, Oracle PL/SQL, Oracle Integration Cloud, Java, JDBC, Servlets, JSP, Spring, Hibernate, HTML5, CSS3, JavaScript, TypeScript, NodesJS, Angular, MySQL, Oracle WebLogic Server, JUnit, JMeter, Selenium Web Driver, etc. He is a certified: Oracle Certified Professional Java SE 6 Programmer, Oracle ADF 11g Certified Implementation Specialist, Oracle Cloud Platform Application Integration 2020 Certified Specialist, Oracle Cloud Infrastructure Foundations 2020 Certified Associate, and Oracle Cloud Infrastructure Developer 2020 Certified Associate

Leave a Reply

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