File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes how to remove whitespace before inserting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to remove whitespace before inserting" Watch "how to remove whitespace before inserting" New topic
Author

how to remove whitespace before inserting

indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
Hi,

am trying to insert values into database, which contains a foreign key...


am sure, the values are exactly same, because am getting database primary key table values in javascript and inserting them into foreign key table in submit.jsp

When I try to print the values.. it shows a lot of whitespaces. How to get rid of these whitespaces before inserting ??? and is this the reason for that error?



thanks,
indu
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

HTH
java.lang.String#trim()


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

Are you using char or varchar for the field? If the former, it adds whitespace to the end and you'll need to trim after getting it out of the database too.


[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
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
I already used trim(). It's not whitespaces.. it is a newline..
A newline is adding for each field am getting from database ....

In database the field is --- varchar...
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

indu yeturu wrote:I already used trim(). It's not whitespaces.. it is a newline..

You have to be specific and need to tell us the details.

Use java.lang.String#split() to split over the pattern "/r/n" (verify this pattern, I'm not sure) and then join the each array element.
indu yeturu
Ranch Hand

Joined: Aug 27, 2008
Posts: 123
thank you for considering... to be in detail...

am using this function to get the elements (which are stored as context attributes) from servlet.
these context attributes are the elements taken from database...

when I populate these values, the dropdown box is populating fine and when I try to Save into database, it is throwing the error as ...


when I alert these values a blank line is appearing for each option which are populating from servlet..>
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

In your AJAX server side action, just remove the new line delimiters coming from the SLECECT OPTIONs value. How to remove that, I already explain in my previous post.

Also, before actually executing the query thru JDBC, print it in console, which give you the clear idea about what query is DB trying to execute and you can debug the same.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to remove whitespace before inserting
 
Similar Threads
XML replacing RDBMS
read-test-insert, keep field unique
while inserting into database
insert new parent/child row. How to handle new primary/foreign key inserts
problem while inserting values