Solved

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

Posted on 2007-04-06
11
1,226 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: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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
The viewer will learn how to implement Singleton Design Pattern in Java.

820 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