Solved

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

Posted on 2007-04-06
11
1,241 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

734 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