Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

XMLConcat in Java

Hi Experts

I've requirement to construct a valid XML instance using XMLConcat operator on multiple xmltypes. Then update XMLType column. So far, able to do this using PLSQL procedure.

    PROCEDURE addAllBaselineObjects (
        p_pguid                             IN VARCHAR2,
        p_pversion                          IN VARCHAR2,
        p_final_baseline_objects_table_type OUT basic_baseline_objects_table_type
    ) IS
    xmlCnt XMLType;
    BEGIN
       dbms_output.put_line('Start addAllBaselineObjects '|| g_baseline_objects_repo_table_type.count);
       
        FOR i in g_baseline_objects_repo_table_type.first..g_baseline_objects_repo_table_type.last LOOP
            SELECT xmlConcat(xmlCnt,XMLELEMENT ("object",
            
                                    XMLElement(
                                        "meta", 
                                        XMLAttributes(g_baseline_objects_repo_table_type(i).guid AS "content", 'GUID' AS "name")
                                        ),
                                    XMLElement(
                                        "meta", 
                                        XMLAttributes(g_baseline_objects_repo_table_type(i).keys AS "content", 'key' AS "name")
                                        )
                                ))
            INTO xmlCnt
            FROM dual; 
        END LOOP;
                    
            UPDATE default_file_revision dfr
            SET dfr.xml_content= 
            XMLQuery('copy $i := $p1 modify
              (for $j in $i//xml/acms-publications
               return (# ora:child-element-name object #)
                      {insert node $p2 into $j})
            return $i'
            PASSING dfr.xml_content AS "p1",
                    XMLElement("baseline",xmlCnt)  AS "p2"
            RETURNING CONTENT)
            where dfr.file_revision_id = (
                SELECT MAX(conref.file_revision_id)
                    FROM CONTENT_REFERENCES conref
                    WHERE conref.entry_name=p_pguid
                    AND conref.version=p_pversion
            );

Now I need to do the same job in java. I did something below. Some how getting null values xmlConcat method.

It would be great if you can suggest any clue.

private void addBaselineInfo(List<BaselineAttributes> addBaselineList, String pubGuid , String pubVersion) { 
String outParam = null; 
XMLType xmlCont = null; 
String xmlElement = null; 
SQLXML xmlObject = null; 
for(int i=1;i<3;i++) { 
BaselineAttributes baselineAttributes = new BaselineAttributes(); 
baselineAttributes.guid = "GUID-"+i; 
baselineAttributes.branchNumber = ""+i; 
baselineAttributes.title = "Title"+i; 
addBaselineList.add(baselineAttributes); 
} 

String addBaselineObjsQuery = "UPDATE default_file dfr\r\n" 
+ " SET dfr.xml_content= \r\n" 
+ " XMLQuery('copy $i := $p1 modify\r\n" 
+ " (for $j in $i//xml/acms-pub\r\n" 
+ " return (# ora:child-element-name object #)\r\n" 
+ " {insert node $p2 into $j})\r\n" 
+ " return $i'\r\n" 
+ " PASSING dfr.xml_content AS \"p1\",\r\n" 
+ " XMLElement(\"baseline\"," + xmlObject + ") AS \"p2\"\r\n" 
+ " RETURNING CONTENT)\r\n" 
+ " where dfr.file_revision_id = (\r\n" 
+ " SELECT MAX(conref.file_id)\r\n" 
+ " FROM CONTENT_REFERENCES conref\r\n" 
+ " WHERE conref.entry_name='" + pubGuid + "'\r\n" 
+ " AND conref.version='" + pubVersion+ "'\r\n" 
+ " )"; 

for (Iterator iterator = addBaselineList.iterator(); iterator.hasNext();) { 
BaselineAttributes baselineAttributes = (BaselineAttributes) iterator.next(); 
xmlElement = " XMLELEMENT (\"object\",\r\n" 
+ " \r\n" 
+ " XMLElement(\r\n" 
+ " \"meta\", \r\n" 
+ " XMLAttributes(" +baselineAttributes.guid+ " AS \"content\", 'GUID' AS \"name\")\r\n" 
+ " ),\r\n" 
+ " XMLElement(\r\n" 
+ " \"meta\", \r\n" 
+ " XMLAttributes(" + baselineAttributes.keys +" AS \"content\", 'key' AS \"name\")\r\n" 
+ " )\r\n" 
+ " )"; 

String xmlTypeConcat = " SELECT xmlConcat(" + xmlObject +", "+ xmlElement +")\r\n" 
+ " AS xmlCnt\r\n" 
+ " FROM dual \r\n"; 
try ( Connection dbConnection = AcmsGenericDAO.getInstance().getConnectionWithLabel("ABC"); 
PreparedStatement pstmt = dbConnection.prepareStatement(xmlTypeConcat ); 
ResultSet rs = pstmt.executeQuery() 
){ 
logger.info("xmlTypeConcat : {}", xmlTypeConcat ); 
while (rs.next()) { 
xmlCont = (XMLType)rs.getObject(1); 
} 

} catch (UniversalConnectionPoolException | SQLException exception) { 
exception.printStackTrace(); 
logger.error(exception.getMessage(),exception); 
} 


} 

Once I've XML Object ready, I can use the same in UPDATE query.

Thanks and regards,

Samba.

Comments

Sdhamoth-Oracle Oct 16 2024

Check the error reported in “Control panel→System and Securities→Administrative Tools→Event Viewer” under Windows Log→Application.

How did you set the Instant Client path in PATH? In command prompt?

If you have set it in a command prompt, then make sure you start that command prompt as administrator (i.e. opening it by right clicking on command prompt icon and select “run as administrator” from popped context menu). Also, you should start ODBC data source administrator by executing “odbcad32” from that command prompt. Have you?

pczurak-JavaNet Oct 16 2024

Yes I started the cmd as run as Administrator.

I am using ODBC 64 bit not 32 bit.

I set the path using Windows UI (System properties), and if I open a cmd window and type set I can see the correct path.

starting “odbcad32” from the command line did not help.

I tried the same process on a different computer and got the same results.

Sdhamoth-Oracle Oct 16 2024

odbcad32 is for ODBC 64 bit on Windows x64 versions. You need to start odbcad32 from syswow64 directory to configure ODBC 32 bit on Windows x64 versions.

Have you checked the Application error logs in Event viewer? Did you find any error related to your ODBC application?

pczurak-JavaNet Oct 16 2024

I am running the 64bit version, if I start odbcad32 from syswow64 it starts the 32bit version.

There are no errors in the Event Viewer.

pczurak-JavaNet Oct 17 2024

I have tried basic driver version 19.24, but when I click on the Test button, I get the following error:

---------------------------
Testing Connection
---------------------------
Unable to connect
SQLState=S1000
[Oracle][ODBC][Ora]ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA

---------------------------
OK
---------------------------

Sdhamoth-Oracle Oct 17 2024

Looks like the service name is missing in your connection string.

Here is a sample of connection string:

sales=
(DESCRIPTION=
(ADDRESS= (PROTOCOL=tcp)(HOST=sales-server)(PORT=1521))
(CONNECT_DATA=
(SERVICE_NAME=sales.us.acme.com)))
The part that is highlighted with bold characters seems to be missing in your connection string. Fix it.

pczurak-JavaNet Oct 17 2024

This is what I am using.

JDEPRD =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = testoda0)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = jdeprd.test.com)
)
)

Sdhamoth-Oracle Oct 17 2024

Are you able to connect to your database using SQL*Plus?

pczurak-JavaNet Oct 17 2024

No:

F:\instantclient_23_5>sqlplus

SQL*Plus: Release 19.0.0.0.0 - Production on Thu Oct 17 07:55:39 2024
Version 19.24.0.0.0

Copyright (c) 1982, 2024, Oracle. All rights reserved.

Enter user-name: testuser
Enter password:
ERROR:
ORA-12560: TNS:protocol adapter error

Enter user-name:

pczurak-JavaNet Oct 17 2024

The issue is fixed

In the ODBC Driver Configuration.

For the TNS Service Name you cannot type the name even if you type it exactly as it shows in the dropdown, you must select it from the dropdown, otherwise it won't work.

1 - 10

Post Details

Added on Mar 22 2024
0 comments
115 views