Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 585
Kanishka Gupta Avatar asked Jun 07 '13 21:06

Kanishka Gupta


People also ask

How do you get values from ResultSet?

The ResultSet interface declares getter methods (for example, getBoolean and getLong ) for retrieving column values from the current row. You can retrieve values using either the index number of the column or the alias or name of the column. The column index is usually more efficient. Columns are numbered from 1.

How do you find the information about columns in a ResultSet object?

You can get the column count in a table using the getColumnCount() method of the ResultSetMetaData interface. On invoking, this method returns an integer representing the number of columns in the table in the current ResultSet object.

What is the return type of ResultSet?

This method returns a boolean value specifying whether the ResultSet object contains more rows. If there are no rows next to its current position this method returns false, else it returns true.

Is ResultSet a string?

A ResultSet isn't a string, or anything resembling one. Logically speaking it is an array of maps. You need to traverse it, getting column values for each row, and do whatever you need to do with those.


1 Answers

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)
like image 177
ddmps Avatar answered Oct 02 '22 13:10

ddmps