Skip to Main Content

New

How to Integrate Gemini AI with Oracle APEX (Step-by-Step Guide)

Published on 21 Mar 2026 by CHATURVEDINIDHI26@GMAIL.COM


πŸš€ How to Integrate Gemini AI with Oracle APEX (Step-by-Step Guide)

In today’s world, users don’t want to write complex SQL queriesβ€”they want instant answers in plain English.

What if your Oracle APEX application could understand natural language and automatically generate SQL queries, reports, and insights?

In this blog, I’ll show you how to connect Google Gemini AI with Oracle APEX and build a powerful AI-powered search engine πŸ”₯


πŸ’‘ What We Are Building

πŸ‘‰ A smart APEX application where users can:

  • Ask questions in plain English
  • Automatically generate Oracle SQL
  • View results in reports/charts
  • No SQL knowledge required

🧠 Architecture Overview

User Input β†’ Gemini AI API β†’ SQL Query β†’ Oracle APEX Report


πŸš€ Step 1: Get Gemini API Key (Free)

  1. Go to Google AI Studio
  2. Login with your Google account
  3. Click Create API Key
  4. Copy the key

πŸ‘‰ No credit card required βœ…


βš™οΈ Step 2: Create APEX Page Items

Create these items:

  • P1_SEARCH β†’ User input
  • P1_RESPONSE β†’ Store API response
  • P1_SQL β†’ Extracted SQL

πŸ”— Step 3: Call Gemini API using PL/SQL

Use APEX_WEB_SERVICE to call the API:

DECLARE
    l_response CLOB;
BEGIN
    apex_web_service.g_request_headers.delete;

    apex_web_service.g_request_headers(1).name  := 'x-goog-api-key';
    apex_web_service.g_request_headers(1).value := 'YOUR_API_KEY';

    apex_web_service.g_request_headers(2).name  := 'Content-Type';
    apex_web_service.g_request_headers(2).value := 'application/json';

    l_response := apex_web_service.make_rest_request(
        p_url => 'https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent',
        p_http_method => 'POST',
        p_body => '{
          "contents": [
            {
              "parts": [
                {
                  "text": "You are an Oracle SQL expert. Return ONLY SQL query. Table: XXQST_INVENTORY (ITEM_NAME, PRICE, QUANTITY, CATEGORY). User Query: ' || :P1_SEARCH || '"
                }
              ]
            }
          ]
        }'
    );

    :P1_RESPONSE := l_response;
END;

πŸ” Step 4: Extract SQL from Response

Gemini returns JSON, so we extract SQL:

DECLARE
    l_sql VARCHAR2(4000);
BEGIN
    l_sql := REGEXP_SUBSTR(:P1_RESPONSE, 'SELECT.*', 1, 1);
    :P1_SQL := l_sql;
END;

πŸ“Š Step 5: Display Data in APEX Report

Create an Interactive Report

Source:

RETURN :P1_SQL;

🎯 Example

User Input:

Show products where price > 5000

AI Output:

SELECT * FROM XXQST_INVENTORY WHERE PRICE > 5000;

πŸ‘‰ Report auto-generates instantly πŸš€


πŸ’‘ Pro Tips (Very Important)

πŸ”₯ 1. Use Strong Prompt

Return ONLY SQL query.
Do NOT explain.
No markdown.

πŸ”₯ 2. Define Table Structure

Always pass table + columns to AI

πŸ”₯ 3. Handle Errors

  • Invalid SQL
  • Empty responses
  • API limits

⚠️ Common Issues

IssueSolution
404 errorUse correct model
No responseCheck API key
Wrong SQLImprove prompt

πŸ”₯ Final Result

You now have:

βœ… AI-powered search
βœ… Natural language β†’ SQL
βœ… Auto reports
βœ… Zero SQL dependency


πŸš€ What’s Next?

You can extend this further:

  • πŸ“Š Auto charts & dashboards
  • 🎀 Voice-based search
  • 🌐 Multi-language queries
  • πŸ“ˆ KPI cards

πŸ’¬ Final Thoughts

Integrating AI with Oracle APEX opens a new world of possibilities.
You’re no longer building just appsβ€”you’re building intelligent systems.

πŸ‘‰ This is how modern Business Intelligence should feel.


πŸ”₯ If you found this helpful...

Follow for more Oracle APEX + AI content πŸš€
And feel free to connect!


#OracleAPEX #AI #Gemini #SQL #LowCode #Automation #DataAnalytics

πŸ“ Nidhi Blog

Sharing knowledge, tutorials, and stories about Oracle APEX, development, and digital growth.

πŸ“š Quick Links
πŸ“§ Contact

Email: support@nidhispace.com

Follow us on:
linkedin | YouTube


Β© Nidhi Blog. All rights reserved.