Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-06
11
Medium Priority
?
1,266 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

704 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