• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Update problem

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Mohamed Manas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so you know you must be passing twenty characters of data to the database.

What's this:

line doing?
 
Mohamed Manas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mohamed Manas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where/how are you seeing these space characters? In other words, how did you notice they're there?
 
Mohamed Manas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/GeneralJdbcQuestions points you to Microsoft's own driver. jTDS is also an option.
 
Mohamed Manas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:http://faq.javaranch.com/java/GeneralJdbcQuestions points you to Microsoft's own driver. jTDS is also an option.



thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic