简体   繁体   中英

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.

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)

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) .

Mapping of char is to String. char映射到String。 So there exists getString(String column_name) for the char data type

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.

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. :)

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM