aspose file tools*
The moose likes JDBC and the fly likes Inserting junk values while  using Prepared Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting junk values while  using Prepared Statement" Watch "Inserting junk values while  using Prepared Statement" New topic
Author

Inserting junk values while using Prepared Statement

Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Hi all,
I am using oracle 8.1.7 and oracle 9i jdbc driver.I am inserting multiple records using prepared statement.I gave auto commit false and i am iterating each record and setting value using prepared statement and calling execute update.Finally after the end of the loop i am doing commit and made setAutoCommit(true).But it is inserting junk values for a particular column in the database. The code is given below

Regards
Vasu
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
Make sure you insert the right data into a field. If there is a function which database uses before inserting data into a field, you need to call that function in your sql statement. For instance, "insert into customer(id,name) values (HEXTORAW(?),?)" where HEXTORAW is the function that database uses to convert a hex number to a raw number before inserting the record.
Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
No i didnt use any function
Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Hi all,
Someone help me to solve this problem.
Regards
Vasu
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Vasu,
Can you post the SQL you are using?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
is this all true:
1. the first column with a ? in your PreparedStatement is a char or varchar (or some similar text-based) dataype into which you can safely insert a constant string? In other words, if you change your SQL (strsql) to replace the first ? with a constant value (such as 'hello world'), and then comment out your first pst.setXXX() and renumber the rest, do you get that fixed string value inserted reliably?
2. ut.trim(i.getIssueTitle()) returns a String. (although here you ought to get the same behavior of an automatic toString() within the pst.setString() method call as you get in the System.out.println() call....)
3. The string you're trying to insert has no control characters in it (backspaces, etc.) - if you're on UNIX, redirect the System.out.println to a file and then use "od -c <filename>". On PC, try to open it in a dumb editor that doesn't handle special characters well.... Windows notepad should show little squares or something.
4. Assuming #1, the string you're trying to insert is small enough to fit in the column specified (not 'Hello World' into varchar(3) or something similar)
5. The 9i JDBC driver says it's compatible with the 8.1.7 DB Server? (I might check this first...)
If all of those are true, check the release notes for the JDBC driver, see if there's a known issues list online, search Google Groups....
And, yes, pls post the SQL, and possibly the table structure - that'd help.
Good luck,
Jon Egan
Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Hi jon,

Thanks for suggestion but i already tried all the things before posting the qusetion in forum. I hardcoded some value in prepared statement but still it inserts some junk values into table. As i said earlier i even tried printing the value i am passing it and it returns the correct value. Problem is that prepared statement is not setting the correct value. I am facing similar type of problem in more than one screens in my project.Intresting thing is that prepared statement for same table is working perfectly for insert statements but it is not working for update statements.

Regards
Vasu
Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Hi all,
Any body knows about this problem...
Regards
Vasu
Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Please help me on this problem...
Regards
Vasu
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Vasu,
Can you post the SQL you are using? More information would help in providing you a more detailed response.
Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Hi jeanne,
This is the update query i am using .

Regards
Vasu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting junk values while using Prepared Statement