Solved

JDBC rst.getString()

Posted on 2002-05-01
5
241 Views
Last Modified: 2006-11-17
I have a table where a column is defined as a CHAR(50).

When I use JDBC to get a recordset from this table, and then use getString to get the value of that column, I get a string that is padded with white spaces to complete the 50 characters even if the value in the cell is only 2 characters.

Is there a way to just get back the exact value from the table without then having to trim the String?
0
Comment
Question by:afpcos
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6984494
As your column is defined as CHAR(50) I'd say that what it is returning IS the exact value from the table.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6984667
What objects is getting at is that's how CHAR fields are meant to work - they're of a fixed size and padded. That leaves you two options:

1. Perform the trim at the database level with SELECT TRIM(some_column) FROM some_table.
2. Perform the trim at the application level.

Since SQL is notoriously DBMS-specific, making it a possibility that the TRIM() function is not implemented in your DBMS, you would be safer to do it at the application level. On the other hand, if it is supported in your RDBMS, you might find it enhances performance to do it at the DB.
0
 
LVL 3

Expert Comment

by:rjackman
ID: 6985099
either u trim the String in your java code or trim it in sql
cheers
RJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6985329
You should experiment with trimming at the DB versus trimming at your application, as that's the only way to discover the better performing option. The latter course will always be more portable though.
0
 
LVL 4

Accepted Solution

by:
sumit_only earned 100 total points
ID: 6985569
If possible, then change the definition of the column from CHAR(50) to VARCHAR2(50). Then u wud not need any trimming. But that depends upon whether u can modify the definition now or not. Otherwise, u wud have to trim the string.
Kind regards
Sumit
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 83
Groovy:unable to resolve class error 2 70
thymeleaf natural templating vs JSP 2 69
Why my table column Id is not passed to java object? 4 28
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now