Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values of a column without knowing the column type

I am creating a query dynamically based on a user choosing fields to be displayed in the result. The problem is that when data is to be retrieved from the database I normally use getXXX() methods to retrieve the appropriate type. But in this case I do not know what the appropriate type is since the columns are randomly chosen.

Is there any way that I can get data in string format without specifying the data type XXX? I am using MySQL and Servlets.

like image 933
Vini Avatar asked Mar 09 '13 12:03

Vini


1 Answers

You can use the generic getObject method :

Gets the value of the designated column in the current row of this ResultSet object as an Object in the Java programming language.

This method will return the value of the given column as a Java object. The type of the Java object will be the default Java object type corresponding to the column's SQL type, following the mapping for built-in types specified in the JDBC specification. If the value is an SQL NULL, the driver returns a Java null.

like image 152
Denys Séguret Avatar answered Oct 20 '22 19:10

Denys Séguret