PostgreSQL trailing spaces

I have a problem with inserting and updating database in postgresdatabase.

When I do something with database using PreparedStatement every string has trailing spaces at the end. How to avoid this situation?

Example:

PreparedStatement p = con.prepareStatement("insert into hospital_user (login, callmanager_user, pin, css, user_type_id, surname, name, phone) values (?, ?, ?, ?, ?, ?, ?, ?)");

p.setString(1, this.login);

p.setString(2, this.callmanagerUser);

p.setInt(3, this.pin);

p.setString(4, this.css);

p.setInt(5, this.userTypeId);

p.setString(6, this.surname);

p.setString(7, this.name);

p.setInt(8, this.phone);

if (1 == p.executeUpdate()) {

return "ok";

}

althoug this.login = 'foo' it is inserted as 'foo' (trailing spaces at the end) if login is defined in DB as varchar(20) I have foo with 17 spaces at the end.

I use Postgres 7.4 and JDBC postgresql-8.2-504.jdbc3.jar

Any ideas hov to trim this spaces?

[1028 byte] By [Wierzbaa] at [2007-11-26 16:18:16]
# 1

The spaces won't get inserted unless they're in your string to begin with. Either you're checking for their existence in the wrong manner (probable) or you're acquiring a string with extra spaces somehow and inserting that.

In the latter case you can just use the String object's trim method to get an appropriately reformatted string and insert that.

dcmintera at 2007-7-8 22:41:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Now I use getter method to aquire String example

public String getLogin() {

if(login == null) return login;

else return login.trim();

}

and then

p.setString(1, getLogin());

and it does nothing :( I have trailing spaces :(

Wierzbaa at 2007-7-8 22:41:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
> and it does nothing :( I have trailing spaces :(Bet you you just think you do. What leads you to believe you've got trailing spaces?
dcmintera at 2007-7-8 22:41:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
Are your database columns declared as fixed length or variable length?
DrClapa at 2007-7-8 22:41:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
I have fixed string length ex. varchar(20)The way to find out that i have trailing spaces is to type it in shell clientselect login || ' ' || name from hospital_user;results login (many spaces) name
Wierzbaa at 2007-7-8 22:41:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Hmm. Are you using the psql command line client to do your select? It looks like it, so you're probably not doing the thing I thought you were doing (sounds like you're way ahead of me actually). You might get more luck from Dr C.

Even so, I'd be interested to know if you're definitely getting spaces in the column, not just an artifact of how you're displaying the results. If you write a little test client that pulls back the data using a prepared statement, and then does a length() call on the resulting string, do you see the text length, or the column length (20) ?

Of course it's always possible that you've actually got a bunch of leading spaces on the name field instead :-)

D.

dcmintera at 2007-7-8 22:41:28 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...