JDBC rst.getString()

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?
LVL 2
afpcosAsked:
Who is Participating?
 
sumit_onlyConnect With a Mentor Commented:
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
 
objectsCommented:
As your column is defined as CHAR(50) I'd say that what it is returning IS the exact value from the table.
0
 
CEHJCommented:
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
 
rjackmanCommented:
either u trim the String in your java code or trim it in sql
cheers
RJ
0
 
CEHJCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.