my dog learned polymorphism*
The moose likes JDBC and the fly likes Update problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Update problem" Watch "Update problem" New topic
Author

Update problem

Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
hi all,
I am developing a small Movie Booking system as an assignment of my degree programe.

I am having a problem with updating a database with a new value.

This is the coding I used to update the database.



My problem is if I give "pass" as my new password, it stores it as "pass________________" ( _ is for a blank space. so "pass" and 16 blank spaces)
Note that my password column's data type is VARCHAR(20).
And the database I am using is Microsoft SQL Server 2003


It is better to make mistakes now and on purpose than later and accidentally...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

There is nothing in JDBC or SQL Server that will cause this behaviour. Are you sure of the value you are passing? Are you sure your data type is varchar and not char? How are you testing this?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
Paul Sturrock wrote:There is nothing in JDBC or SQL Server that will cause this behaviour. Are you sure of the value you are passing? Are you sure your data type is varchar and not char? How are you testing this?


Im sure data type is VARCHAR



These are the coding that I used to take the value from a JPasswordField.







Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

OK, so you know you must be passing twenty characters of data to the database.

What's this:

line doing?
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
Paul Sturrock wrote:OK, so you know you must be passing twenty characters of data to the database.

I just read the password from the JPasswordField and send it to the changePassword method.

Paul Sturrock wrote:
What's this:

line doing?


reference name of the JPasswordField instance is newPasswordField
since the getPassword() method returns an array of char values, I converted it to String with the above line..

still cant figure out the problem
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
You could put a few System.out.println statements along the way between the code that reads the password from the GUI, and the code that writes it to the DB. That way you know what gets entered, whether it gets changed along the way, and what is passed to the DB.


Ping & DNS - my free Android networking tools app
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
Ulf Dittmer wrote:You could put a few System.out.println statements along the way between the code that reads the password from the GUI, and the code that writes it to the DB. That way you know what gets entered, whether it gets changed along the way, and what is passed to the DB.


Of course I tried that just before writing the new password to the database.

it is printing the password without any blank spaces.

Another interesting point is if i try to add a password with 3 characters (say "new") it is adding 17 whitespaces.
if i try to add a password with 19 characters then it adds just another whitespace.

Since the datatype VARCHAR(20) is defined in the SQL Server and the java coding has no knowledge about the number 20, I think the problem is in the database side. Still confused...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Its definately not the database. VARCHAR is variable character length. If you had chosen char (fixed character length) you might see this behaviour.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
Where/how are you seeing these space characters? In other words, how did you notice they're there?
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
Paul Sturrock wrote:Its definately not the database. VARCHAR is variable character length. If you had chosen char (fixed character length) you might see this behaviour.


Of course, yes.

But, this time I changed the datatype to VARCHAR(25), and got the password ("pass") saved with 21 whitespaces. still confusing...

But I agree with you, VARCHAR is of variable length, strange behavior..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hmm. A quick test against SQL Server confirms varchar data types do not normally behave like this (as per the documentation) and I can't think of a way to do it without using a function. This is however the behaviour you get if you use a char data type.
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
Ulf Dittmer wrote:Where/how are you seeing these space characters? In other words, how did you notice they're there?


this code is about changing a password. so after changing the password I tried to login with the new password, but I couldnt login. so I checked in the database itself (SQL Server Managment Studio) and figured out that space characters are inserted after the password..

sorry for the late reply.
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
thanks for all your valuable help here.

Finally found out its a bug in the jdbc_odbc_bridge. So I gave up using prepared statements..

thanks again...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
Since you're using SQLServer, why are you using the JDBC/ODBC bridge instead of a proper type 4 driver? It's free, not buggy like the bridge, multi-thread-capable etc. etc.
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
Ulf Dittmer wrote:Since you're using SQLServer, why are you using the JDBC/ODBC bridge instead of a proper type 4 driver? It's free, not buggy like the bridge, multi-thread-capable etc. etc.


Can you give me more information regarding this. Actually I do not know to connect to the database other than through the JDBC/ODBC bridge.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
http://faq.javaranch.com/java/GeneralJdbcQuestions points you to Microsoft's own driver. jTDS is also an option.
Mohamed Manas
Greenhorn

Joined: Jun 02, 2008
Posts: 24
Ulf Dittmer wrote:http://faq.javaranch.com/java/GeneralJdbcQuestions points you to Microsoft's own driver. jTDS is also an option.


thanks
 
 
subject: Update problem