Problem with entering data

I have a windows Xp system with Microsoft SQL Server 2005 setup and using java 1.6

I'm using the JDBC-ODBC Bridge and also setup the DSN and that works fine.

The following code worked fine a few days back but now every time I enter a row of data and if the data type of a variable isvarchar orchar it enters the text followed by spaces.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:Mydata","myusername","mypassword");

PreparedStatement stat2=con.prepareStatement("insert into Login(vUserName,vPassword,vAddress,age) values(?,?,?,?)");

stat2.setString(1,textName.getText());

stat2.setString(2,textPass.getText());

stat2.setString(3,textAddress.getText());

stat2.setInt(4,Integer.parseInt(textAge.getText()));

stat2.executeUpdate();

JOptionPane.showMessageDialog(frame,new String("Your details have been registered"));

If I enter a name such asJohn in the textName field, when it updates the database, it adds spaces afterJohn upto the max capacity. (The vUserName in the database is a VARCHAR(25) ).

Could this be a problem with the odbc driver or the jdbc driver?

P.S The username and password are bogus here.

Thanks in advance

Message was edited by:

ArcherKing

[1543 byte] By [ArcherKinga] at [2007-11-27 4:57:10]
# 1
try thisstat2.setString(1,textName.getText().trim());stat2.setString(2,textPass.getText().trim());stat2.setString(3,textAddress.getText().trim());
amol_0008@rediffmail.coma at 2007-7-12 10:12:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
The output of TextName.getText() is the right size but when it is entered into the database the extra spaces get added.exampleif I enter John, the TextName.getText() returns only John without spacesbut when the stat2.executeUpdate() is called it adds the extra spaces.
ArcherKinga at 2007-7-12 10:12:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Howdy people! I've finally fixed the problem. I've cornered the b***ard. I thought of posting the solution here just in case some one else runs into the same problem and gets stuck.

It is an option in the DSN setup of the ODBC driver.

[url #" style="display: block;

background-image:url('http://www.geocities.com/lijoethomas/sqlpadding.gif');

width:509px; height: 354px] [/url]

The picture above is one of the pages in the DSN setup for the sql server.

If the ANSI nulls, padding and warning option is enabled it pads your VARCHAR's and CHAR's with white space.

VARCHAR's are supposed to be just the characters we enter.

Foot note

*** stands for "ast"

Cheers ;)

Message was edited by:

ArcherKing

ArcherKinga at 2007-7-12 10:12:33 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...