Solved

java datatypes for float/double/xmltype when inserting into oracle using PreparedStatement

Posted on 2007-04-06
11
1,252 Views
Last Modified: 2013-12-18
Trying to insert longitude/latitude values into a NUMBER column in oracle and a java string of xml into an XMLTYPE column in oracle using the following code:

                SiteReport siteReport = (SiteReport)siteReports.get(i);
                 String sql = "INSERT INTO SRP_SURVEY_REPORT " +
                "(SRP_PK, SRP_LONGITUDE, SRP_LATITUDE, SRP_XML)" +
                "VALUES (SEQ_SRP_SURVEY_REPORT.nextval,?,?,?)";
               
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setDouble(1, siteReport.getLongitude());  //SiteReport.logitude: tried long, double, Double, float
                ps.setDouble(2, siteReport.getLatitude()); //same
                ps.setObject(3, siteReport.getXml()); //SiteReport.xml is String.
                ps.execute();
                ps.close();


I get the following error every time:
ORA-01461: can bind a LONG value only for insert into a LONG column

Also, what java data type do I use for XMLTYPE??
0
Comment
Question by:jstretch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18864118
>> ps.setDouble(1, siteReport.getLongitude());  //SiteReport.logitude: tried long, double, Double, float

Try with ps.setLong ( 1, siteReport.getLongitude () ) ; // make sure getLongitude () returns a long

Otherwise try casting it:

ps.setLong ( 1, ( long ) siteReport.getLongitude () ) ;
0
 
LVL 6

Author Comment

by:jstretch
ID: 18864300
I tried Long and long, same results. However, after doing some googling on this error I found that its not really not a 'Long' problem, but thats just the message it spits out. So I commented out the third paramter (the XMLTYPE) and I did not get an error...so the  ps.setObject(3, siteReport.getXml()) is whats causing the error.

Now I just have to figure out what java data type to bind to the oracle XMLTYPE, or maybe its a character set incompatibility or size limitiation.
0
 
LVL 6

Author Comment

by:jstretch
ID: 18864318
The getXml is 8100 byte string
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 30

Expert Comment

by:Mayank S
ID: 18864325
If getXml () returns a string, you need to use setString () for it, and the data type in the database should be CHAR or VARCHAR or VARCHAR2 or something of that family
0
 
LVL 6

Author Comment

by:jstretch
ID: 18864605
I need to change the java type instead of the database column. I believe they need to use XMLTYPE for indexing purposes.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18864623
Guess you need to use XMLType () defined by an Oracle library and use an instance of that with setObject ():

http://www.xquery.com/compare/comparison.html
0
 
LVL 6

Author Comment

by:jstretch
ID: 18864679
The XMLTYPE parses a string on insert.

I tried this with error "expect < instead of y":
ps.setString(3, "yabba dabba doo");

 I tried this and no errors:
ps.setString(3, "<root><test>testing</test></root>");

But there is no data in the table still, but thats probably another issue.

So I think the string is too long for the field.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18864740
You need to use:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance () ;
DocumentBuilder db = dbf.newDocumentBuilder () ;
Document doc = db.parse ( new InputSource ( new StringReader ( "<root><test>....</test></root>" ) ) ;
XMLType xmlIn = new XMLType ( connection, doc ) ;
ps.setObject (3,  xmlIn ) ;



0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 18864747
You can download the required JAR files from:

http://www.oracle.com/technology/tech/xml/xdk/software/prod/xdk_java.html

Or they would be available with your Oracle database installation
0
 
LVL 6

Author Comment

by:jstretch
ID: 18864801
Thats the jar I needed and I found an example finally: example 12-5
http://www.stanford.edu/dept/itss/docs/oracle/10g/appdev.101/b10790/xdb11jav.htm
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18866345
Sample is also there in he link I had posted earlier.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question