Solved

JDBC rst.getString()

Posted on 2002-05-01
5
238 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

746 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

10 Experts available now in Live!

Get 1:1 Help Now