0

I get the results of the query to the database in the form of a ResultSet object. A field in a database of different types, and obviously I don't know what the query will be executed and what the ResultSet will be passed to my function.

Regardless of the data type of the column in the ResultSet, this information can be obtained as a string or a number, or date. To do this, use the methods getString(int), getInt(int), etc.

How to know what type of data is in that column, and depending on it to use different methods to get the data?

a764297
  • 3
  • 2
  • Use a __switch__ control statement following an __instanceof__ statement. – Unihedron Aug 16 '14 at 10:17
  • 1
    It isn't at all obvious that you don't know the query to be expected or the types of the result columns. The premiss of JDBC is that you know both. – user207421 Aug 16 '14 at 10:25
  • @EJP: Hmmm ... so you mean general query applications like sqldeveloper and similar may not be written using JDBC? – user2672165 Aug 16 '14 at 11:13

2 Answers2

1

You can use:

java.sql 
Interface ResultSetMetaData

An object that can be used to get information about the types and properties of the columns in a ResultSet object. Then choose get-method based on info retrieved from ResultSetMetaData. There are examples available. Also look at this How can I get different datatypes from ResultSetMetaData in Java?

Community
  • 1
  • 1
user2672165
  • 2,986
  • 19
  • 27
0

Use to retrieve the value of the getObject method, and then define the type (getClass or instanceof). Note the documentation for the method.

Valeriy
  • 1,365
  • 3
  • 18
  • 45