Solved

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

Posted on 2007-04-06
11
1,178 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
  • 6
  • 5
11 Comments
 
LVL 30

Expert Comment

by:mayankeagle
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
 
LVL 30

Expert Comment

by:mayankeagle
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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:
mayankeagle 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:mayankeagle
ID: 18866345
Sample is also there in he link I had posted earlier.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 56
null value 15 58
backtracking recursion  code 19 40
JList custom Cell Renderer refresh 15 39
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now