This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char - getString() ?

 
kelvin cheung
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i try to get a char type from a table.
but i get : incopatible types
and i am sure that the colum is of the type char in the table.

whats wrong here?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to store "Object" type to "primitive" type. This will not work. Assign the getString() to another String object. That must solve your problem.
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this because the rs is an array and therefore the pointer in the array is of type object?

If so I think you can cast the object that you get from the array back into the actual type you want (in this case a char).
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34378
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin,
Databases use char(x) and varchar(x) to represent strings. You need to use the getString() method as you are doing. If you happen to know your database field is only one character, you can use getString().charAt(0) to get the character.

Chris,
rs isn't really an array. It is an object of some type of list. (I'm not sure if that type is array, but I'm thinking it's not.) The resultset getXXX methods give you a more useful data type. For example, you could get a String, Date, int, ... You can get an int primitive back, but not a char primitive.
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha, thanks Jeanne
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic