473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Java + DB2, import table data

WP
Hello, I've just started to work with DB2 (using the express edition
version 9.5 under windows vista) and I'm accessing it through a java
program (my java's very rusty). The java program loads the driver,
connects and tries to create two tables. That works just fine. Then it
tries to import data into one of the tables but it seems I can't use
an import command with Statement.execute() because an exception is
thrown if I attempt to do this.
Is this possible and, if so, how?

Here's the acutal call that triggers the exception:
statement.execute("IMPORT FROM 'C:/staff.xml.xsd' OF DEL METHOD P(1)
MESSAGES 'C:/messages.txt' INSERT INTO STAFF_AS_XML
(STAFF_AS_XML_COL);");

One of the tables has a single xml column and I have an xml-file I
want to import to that table from the java program. The command works
under control center (but not always, which is weird, but that is a
separate issue).

Thanks for reading and thanks for any replies.
Apr 11 '08 #1
2 7069
WP wrote:
Hello, I've just started to work with DB2 (using the express edition
version 9.5 under windows vista) and I'm accessing it through a java
program (my java's very rusty). The java program loads the driver,
connects and tries to create two tables. That works just fine. Then it
tries to import data into one of the tables but it seems I can't use
an import command with Statement.execute() because an exception is
thrown if I attempt to do this.
Is this possible and, if so, how?

Here's the acutal call that triggers the exception:
statement.execute("IMPORT FROM 'C:/staff.xml.xsd' OF DEL METHOD P(1)
MESSAGES 'C:/messages.txt' INSERT INTO STAFF_AS_XML
(STAFF_AS_XML_COL);");

One of the tables has a single xml column and I have an xml-file I
want to import to that table from the java program. The command works
under control center (but not always, which is weird, but that is a
separate issue).

Thanks for reading and thanks for any replies.
The IMPORT command isn't SQL; it's a "CLP command" (which is why it
doesn't appear under the SQL reference in the InfoCenter, but in a
separate section along with other commands like CREATE DATABASE, LOAD,
etc.)

Before DB2 9, you could only execute CLP commands from the CLP (the db2
command line executable), with the exception of one or two commands.
Starting with DB2 9, you can execute many (but not all) CLP commands
via the ADMIN_CMD() [1] stored procedure, including IMPORT [2], EXPORT
[3] and LOAD [4].

However, one important thing to be aware of with ADMIN_CMD(). The
procedure runs the command *on the server*. Hence, for IMPORT, the
input data file must reside on the server (see [2]).

There's probably some "proper" JDBC method for calling stored
procedures, or I suspect you could just do:

statement.execute("CALL SYSPROC.ADMIN_CMD('IMPORT FROM
''C:/staff.xml.xsd'' OF DEL METHOD P(1) MESSAGES ''C:/messages.txt''
INSERT INTO STAFF_AS_XML (STAFF_AS_XML_COL)')");
[1]
http://publib.boulder.ibm.com/infoce.../com.ibm.db2.l
uw.sql.rtn.doc/doc/r0012547.html

[2]
http://publib.boulder.ibm.com/infoce.../com.ibm.db2.l
uw.sql.rtn.doc/doc/r0023575.html

[3]
http://publib.boulder.ibm.com/infoce.../com.ibm.db2.l
uw.sql.rtn.doc/doc/r0023573.html

[4]
http://publib.boulder.ibm.com/infoce.../com.ibm.db2.l
uw.sql.rtn.doc/doc/r0023577.html
Cheers,

Dave.
Apr 11 '08 #2
WP
On 11 Apr, 15:51, "Dave Hughes" <d...@waveform.plus.comwrote:
[snip]
>
The IMPORT command isn't SQL; it's a "CLP command" (which is why it
doesn't appear under the SQL reference in the InfoCenter, but in a
separate section along with other commands like CREATE DATABASE, LOAD,
etc.)

Before DB2 9, you could only execute CLP commands from the CLP (the db2
command line executable), with the exception of one or two commands.
Starting with DB2 9, you can execute many (but not all) CLP commands
via the ADMIN_CMD() [1] stored procedure, including IMPORT [2], EXPORT
[3] and LOAD [4].

However, one important thing to be aware of with ADMIN_CMD(). The
procedure runs the command *on the server*. Hence, for IMPORT, the
input data file must reside on the server (see [2]).

There's probably some "proper" JDBC method for calling stored
procedures, or I suspect you could just do:

statement.execute("CALL SYSPROC.ADMIN_CMD('IMPORT FROM
''C:/staff.xml.xsd'' OF DEL METHOD P(1) MESSAGES ''C:/messages.txt''
INSERT INTO STAFF_AS_XML (STAFF_AS_XML_COL)')");
Hello Dave and thanks very much indeed for your helpful reply! Using
your suggestions I did this:
private void performImport(Connection db2_con) throws SQLException {
CallableStatement stmt = db2_con.prepareCall("CALL
SYSPROC.ADMIN_CMD(?)");
stmt.setString(1, "IMPORT FROM \"C:/staff.xml.xsd\" OF DEL METHOD P
(1) INSERT INTO STAFF_AS_XML (STAFF_AS_XML_COL)");

stmt.executeQuery();
}

which seems to work just fine. I used Control Center to "perform" an
import and I used the commad string it uses (which it can show me).
However, I had to remove the MESSAGES-stuff or an exception is thrown.
I guess db2 doesn't want to communicate warnings and errors for the
import command when accessing it through jdbc->stored procedure which
is fine by me.
Also, this program is accessing a database running on the same host
and we don't expect this setup to change anytime soon.
Very many thanks again, Dave!
>
[1]http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.l
uw.sql.rtn.doc/doc/r0012547.html

[2]http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.l
uw.sql.rtn.doc/doc/r0023575.html

[3]http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.l
uw.sql.rtn.doc/doc/r0023573.html

[4]http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.l
uw.sql.rtn.doc/doc/r0023577.html

Cheers,

Dave.
- WP
Apr 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
1
by: Larry Menard | last post by:
Folks, I've written the world's simplest java UDF, and it is complaining that it can't load the method. The class seems OK, it's complaining about the method. The JDBC Sample UDFs (e.g.,...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.