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.
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With