Home » Developer & Programmer » JDeveloper, Java & XML » Emergency problem of creating a view of XMLType
icon2.gif  Emergency problem of creating a view of XMLType [message #121555] Mon, 30 May 2005 10:48 Go to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
Hi, All:

I have an emergency problem when create a view of XMLType
using the following SQL script: (any reply also can forward to
steve_luo88@yahoo.ca, actually the original task is to store
the query result in a oracle table and later on can retrieve it
as XML element)

------------------------------
DROP TABLE employees;
CREATE TABLE employees
(empno number(4), fname varchar2(20), lname varchar2(20), hire date, salary
number(6));

INSERT INTO employees VALUES
(2100, 'John', 'Smith', Date'2000-05-24', 30000);

INSERT INTO employees VALUES
(2200, 'Mary', 'Martin', Date'1996-02-01', 30000);

CREATE OR REPLACE VIEW Emp_view OF XMLTYPE WITH OBJECT ID
(EXTRACT(sys_nc_rowinfo$,'/Emp/@empno').getnumberval())
AS SELECT XMLELEMENT("Emp", XMLAttributes(empno),
XMLForest(e.fname ||' '|| e.lname AS "name",
e.hire AS "hiredate")) AS "result"
FROM employees e
WHERE salary > 20000;

------------------------------------
Everything is okay for running the above scripts, but when

Select * from EMP_View, the wrong formatted result as:

<Emp EMPNO="2100">
<name>John Smith</name>
<hiredate>24-MAY-00</hiredate>
</

<Emp EMPNO="2200">
<name>Mary Martin</name>
<hiredate>01-FEB-96</hiredate>
<


2 rows selected.

-------------------

So what's wrong.

I aslo meet the problem when try to load the XDK into Database by running:
$ORACLE_HOME/rdbms/admin/initxml.sql
the system said that it can not open "dbmsxsu.xml" and
"xmlload.xml"

But the system said the initxml.sql procedure successfully
executed.


Any suggestions greatly appreciated!
Re: Emergency problem of creating a view of XMLType [message #121559 is a reply to message #121555] Mon, 30 May 2005 11:19 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
What Oracle release do you use ?
I'm working in 9.2.0.4:

SQL> CREATE OR REPLACE VIEW Emp_view OF XMLTYPE WITH OBJECT ID
  2  (EXTRACT(sys_nc_rowinfo$,'/Emp/@empno').getnumberval())
  3  AS SELECT XMLELEMENT("Emp", XMLAttributes(empno),
  4  XMLForest(e.fname ||' '|| e.lname AS "name",
  5  e.hire AS "hiredate")) AS "result"
  6  FROM employees e
  7  WHERE salary > 20000;

View created.

SQL> select * from emp_view;

SYS_NC_ROWINFO$
-------------------------------------------------------------------------------
<Emp EMPNO="2100"><name>John Smith</name><hiredate>24/05/2000</hiredate></Emp>
<Emp EMPNO="2200"><name>Mary Martin</name><hiredate>01/02/1996</hiredate></Emp>


Rgds.
Re: Emergency problem of creating a view of XMLType [message #121564 is a reply to message #121555] Mon, 30 May 2005 12:59 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Could it be that you only print the first x characters of your column (something like set long 4000 in the old days)?

hth
Re: Emergency problem of creating a view of XMLType [message #121573 is a reply to message #121564] Mon, 30 May 2005 15:35 Go to previous messageGo to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
DROP TABLE employees;
CREATE TABLE employees
(empno number(4), fname varchar2(20),
lname varchar2(20), hire date, salary
number(6));

INSERT INTO employees VALUES
(2100, 'John', 'Smith', Date'2000-05-24',30000);

INSERT INTO employees VALUES
(2200, 'Mary', 'Martin', Date'1996-02-01', 30000);

CREATE OR REPLACE VIEW Emp_view OF XMLTYPE
WITH OBJECT ID
(EXTRACT(sys_nc_rowinfo$,'/Emp/@empno').
getnumberval())
AS SELECT XMLELEMENT("Emp",
XMLAttributes(empno),
XMLForest(e.fname ||' '||
e.lname AS "name",
e.hire AS "hiredate"))
AS "result"
FROM employees e
WHERE salary > 20000;



---by UserName: sluo, Pwd:steveluo88,
Hi, All:

I have an emergency problem when create
a view of XMLType
using the following SQL script:
------------------------------
DROP TABLE employees;
CREATE TABLE employees
(empno number(4), fname varchar2(20),
lname varchar2(20), hire date, salary
number(6));

INSERT INTO employees VALUES
(2100, 'John', 'Smith', Date'2000-05-24', 30000);

INSERT INTO employees VALUES
(2200, 'Mary', 'Martin', Date'1996-02-01', 30000);

CREATE OR REPLACE VIEW Emp_view OF XMLTYPE WITH
OBJECT ID
(EXTRACT(sys_nc_rowinfo$,'/Emp/@empno').
getnumberval())
AS SELECT XMLELEMENT("Emp",
XMLAttributes(empno),
XMLForest(e.fname ||' '|| e.lname
AS "name",
e.hire AS "hiredate"))
AS "result"
FROM employees e
WHERE salary > 20000;

------------------------------------
Everything is okay for running the above
scripts, but when

Select * from EMP_View, the wrong formatted
result as:

<Emp EMPNO="2100">
<name>John Smith</name>
<hiredate>24-MAY-00</hiredate>
</

<Emp EMPNO="2200">
<name>Mary Martin</name>
<hiredate>01-FEB-96</hiredate>
<


2 rows selected.

-------------------

So what's wrong.

I aslo meet the problem when try to load
the XDK into Database by running:
$ORACLE_HOME/rdbms/admin/initxml.sql
the system said that it can not open "dbmsxsu.xml" and
"xmlload.xml"

But the system said the initxml.sql procedure
successfully
executed.


Any suggestions greatly appreciated!


Re: Emergency problem of creating a view of XMLType [message #121574 is a reply to message #121559] Mon, 30 May 2005 15:43 Go to previous messageGo to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
I also work on the Oracle 9.2, why
your query result looks good but mine wrong!

Do you need to set up XDK in Oracle 9.i
and see my problem of running InitXML.sql.
(do you need to set up XDK, or at the very
beginning of install Oracle, by selecting
the options of JDeveloper)

Re: Emergency problem of creating a view of XMLType [message #121579 is a reply to message #121574] Mon, 30 May 2005 16:50 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9088
Registered: November 2002
Location: California, USA
Senior Member
It loks like Frank is correct about the SET LONG, as demonstrated below.

scott@ORA92> SET LONG 80
scott@ORA92> Select * from EMP_View
  2  /

SYS_NC_ROWINFO$
----------------------------------------------------------------------------------------------------
<Emp EMPNO="2100">
  <name>John Smith</name>
  <hiredate>24-MAY-00</hiredate>
</

<Emp EMPNO="2200">
  <name>Mary Martin</name>
  <hiredate>01-FEB-96</hiredate>
<


2 rows selected.


scott@ORA92> SET LONG 5000
scott@ORA92> Select * from EMP_View
  2  /

SYS_NC_ROWINFO$
----------------------------------------------------------------------------------------------------
<Emp EMPNO="2100">
  <name>John Smith</name>
  <hiredate>24-MAY-00</hiredate>
</Emp>

<Emp EMPNO="2200">
  <name>Mary Martin</name>
  <hiredate>01-FEB-96</hiredate>
</Emp>


2 rows selected.

icon7.gif  Re: Emergency problem of creating a view of XMLType [message #121585 is a reply to message #121579] Mon, 30 May 2005 18:36 Go to previous messageGo to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
Thank you, yes, after you "SET LONG", it
works out the right format.

But how to extract the specific records.

see select "empno=2000"
Re: Emergency problem of creating a view of XMLType [message #121621 is a reply to message #121585] Tue, 31 May 2005 02:46 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
sluo wrote on Tue, 31 May 2005 03:36

But how to extract the specific records.
see select "empno=2000"



SQL> select * from emp_view where SYS_NC_ROWINFO$ like '_Emp EMPNO="2100"%';

SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<Emp EMPNO="2100"><name>John Smith</name><hiredate>24/05/2000</hiredate></Emp>

SQL> SELECT * FROM emp_view X
  2  WHERE extractValue(value(X),'Emp/name') = 'John Smith'
  3  /

SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<Emp EMPNO="2100"><name>John Smith</name><hiredate>24/05/2000</hiredate></Emp>

SQL> select * from emp_view X where
  2  existsNode(value(X),'/Emp[name="John Smith"]') = 1
  3  /

SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<Emp EMPNO="2100"><name>John Smith</name><hiredate>24/05/2000</hiredate></Emp>

SQL> select * from emp_view X
  2  where extractValue(value(X),'/Emp/@EMPNO') = 2100
  3  /

SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<Emp EMPNO="2100"><name>John Smith</name><hiredate>24/05/2000</hiredate></Emp>

SQL> select * from emp_view X where
  2  existsNode(value(X),'/Emp[@EMPNO="2100"]') = 1
  3  /

SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<Emp EMPNO="2100"><name>John Smith</name><hiredate>24/05/2000</hiredate></Emp>


Rgds.
Re: Emergency problem of creating a view of XMLType [message #121691 is a reply to message #121621] Tue, 31 May 2005 09:24 Go to previous messageGo to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
Thank you very much for
your great and prompt help!


Steve
Re: Emergency problem of creating a view of XMLType [message #121697 is a reply to message #121621] Tue, 31 May 2005 10:06 Go to previous messageGo to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
Also about how to save the results from joiningtwo tables,
it's possible to use the XMLtype or other "select"
Save as a table, view, later on can update it and use it.
I think it's possible, but can't find the better way.
Re: Emergency problem of creating a view of XMLType [message #121817 is a reply to message #121697] Wed, 01 June 2005 03:43 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
Concerning update question:

SQL> create table xml_tab of xmltype;

Table created.

SQL> insert into xml_tab select value(t) from emp_view t;

2 rows created.

SQL> commit;

Commit complete.

SQL> select * from xml_tab;

SYS_NC_ROWINFO$
--------------------------------------------------------------------------------
<Emp EMPNO="2100"><name>John Smith</name><hiredate>24.05.00</hiredate></Emp>
<Emp EMPNO="2200"><name>Mary Martin</name><hiredate>01.02.96</hiredate></Emp>

SQL> update xml_tab t set value(t) = updateXML(value(t),'/Emp/name/text()','New name')
  2  where extractValue(value(t),'/Emp/@EMPNO') = 2100
  3  /

1 row updated.

SQL> select value(t) from xml_tab t where extractValue(value(t),'/Emp/@EMPNO') = 2100;

VALUE(T)
--------------------------------------------------------------------------------
<Emp EMPNO="2100">
  <name>New name</name>
  <hiredate>24.05.00</hiredate>
</Emp>


Rgds.
Re: Emergency problem of creating a view of XMLType [message #121868 is a reply to message #121817] Wed, 01 June 2005 09:34 Go to previous messageGo to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
Thank you very much.

The last problem is that How can I call these commands in Java.

(In Oracle 9i Developer's guide, there are some introductions,
but it seems too long)
Re: Emergency problem of creating a view of XMLType [message #121874 is a reply to message #121868] Wed, 01 June 2005 09:55 Go to previous messageGo to next message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
sluo wrote on Wed, 01 June 2005 18:34

(In Oracle 9i Developer's guide, there are some introductions,
but it seems too long)


Steve, I'm afraid no other way else to read it.
Shotly, you have to register jdbc driver, create Connection
object, create PreparedStatement and execute statement you need.
Very stupy example below.

    OracleConnection cnn = null;
    try 
    {
      ResultSet rs = null;
      PreparedStatement pstm = null;
      DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
      cnn = (OracleConnection)DriverManager.getConnection("jdbc:oracle:thin:@10.232.1.94:1521:dbg9204","scott","tiger");
      cnn.setAutoCommit(false);
....
  pstm = cnn.prepareStatement("update emp set sal=sal");
  pstm.execute();
  cnn.commit();


Rgds.
Re: Emergency problem of creating a view of XMLType [message #121903 is a reply to message #121874] Wed, 01 June 2005 12:18 Go to previous messageGo to next message
sluo
Messages: 8
Registered: May 2005
Junior Member
Thank you very much, I deeply appreciate your precious help.

I mean that can we use some statements in Java
code as the following:

create or replace view of
XMLType Emp_view OF XMLTYPE WITH OBJECT ID
(EXTRACT(sys_nc_rowinfo$,'/Emp/@empno').getnumberval())
AS SELECT XMLELEMENT("Emp", XMLAttributes(empno),
XMLForest(e.fname ||' '|| e.lname AS "name",
e.hire AS "hiredate")) AS "result"
FROM employees e
WHERE salary > 20000;
or
select * from emp_view where extractValue (value (x),
'Emp/name')='John Smith'

For prepare statement, I understand it and try to use it
in Java.


Re: Emergency problem of creating a view of XMLType [message #121976 is a reply to message #121903] Thu, 02 June 2005 02:58 Go to previous message
dmitry.nikiforov
Messages: 723
Registered: March 2005
Senior Member
In java you can execute any DML statements like
SELECT, UPDATE (including any for XMLType data).
Say, you can call "select * from emp_view where extractValue (value (x),'Emp/name')= ?" where ? will be the parameter
of statement.

What of DDL like CREATE VIEW, you will need to use
constructions like "begin execute immediate <<something>>; end;".
PreparedStatement, Statement and CallableStatement objects in Java don't support DDL commands.

Also you can use DBMS_UTILITY.EXEC_DDL_STATEMENT procedure
and call it from Java through PreparedStatement with String
parameter.

Rgds.
Previous Topic: Passing java Arrays to Stored Procedure in Oracle.
Next Topic: Sample code required
Goto Forum:
  


Current Time: Thu Apr 18 12:52:51 CDT 2024