Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Databases
Expertise: Beginner
Apr 7, 2000

Database Field Types

How do I get a string representation of a database field using JDBC regardless of its type? In other words, I have to use the getInt(), getDate(), getString(), etc., to get a field value depending on the field type in the database. I just want the string representations so I can print them in HTML output to a Web browser.

The JDBC API was designed realizing that many applications built with it would involve dynamically creating Web pages and text reports. Therefore, its designers made it easy to both retrieve database fields as Java data types and also as simple strings. You can use the ResultSet class' getString() method to retrieve any database field type as a String. The JDBC driver will take care of converting the value to a String representation.

Strictly relying on getString() may not be appropriate for every situation. For example, floating point numbers and date/time values often require special formatting before printing them. The default formatting performed by the JDBC driver may be inadequate. Therefore, you will want to fetch some values as Java data types and format them yourself with the java.text package.

DevX Pro
Thanks for your registration, follow us on our social networks to keep up-to-date