Kanishka Gupta
Kanishka Gupta

Reputation: 217

How to get the char data type from mysql using resultset

I have been doing CRUD operation on database, but i could not find any direct method for the getting the data type char in database.

Though i achieved the output using the getString(String column_name) of the result set, but i wonder why there does not exist any method like getChar(String column_name), since String and Character are two different data types.

Upvotes: 10

Views: 39845

Answers (5)

Mrs. Wint Sandi Soe
Mrs. Wint Sandi Soe

Reputation: 1

When the data from table is just one character (CHAR type), I use like this in Java program.

    String str=rs.getString("column_name").toString();

Upvotes: -1

ddmps
ddmps

Reputation: 4380

As MySQL sees it, it's all Strings because it has no type for a single character. Sure, you can set CHAR or VARCHAR to sizes of max one, but this is a special case and you generally don't want to make methods for special cases when the functionality is already there.

Just extract the Java char from the resulting String, as such:

getString(column_name).charAt(0)

Upvotes: 24

Pragmateek
Pragmateek

Reputation: 13374

Databases often have only one type for representing all the strings of characters.

A char is only a special case: a string with one character, so it would not be really useful to add this type.

And don't be fooled by their name like CHAR. :)

Upvotes: 1

greedybuddha
greedybuddha

Reputation: 7507

It is because a Char in MySQL doesn't really mean a singular char value, it really means a array of chars. In java an array of chars is represented as a String, hence the same method.

Upvotes: 5

user1707035
user1707035

Reputation:

Refer to the link and see the table 3-1 and you will get decent idea that why there does not exists getChar(String column_name).

Summary: Mapping of char is to String. So there exists getString(String column_name) for the char data type

Upvotes: 10

Related Questions