• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

returning value of ResultSetMetaData.getColumnType(int column)

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can anybody tell me what will ResultSetMetaData.getColumnType(int column)
returns. It returns an integer, what is that integer. For eg. for column type Varchar2 it returned 12.
Thanks
Neha
 
Sheriff
Posts: 17343
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not rely on the actual int value but on the constants defined in java.sql.Types
[This message has been edited by JUNILU LACAR (edited August 23, 2001).]
 
Neha Sharma
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu,
Could you explain further, I didn't understand what is the interger type of column type. I mean if it is string what it should return, if it is 'Date' type what should this method return.
Thanks
Neha
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the static constants that are defined in the class java.sql.Types.
One of these static integer constants is what is returned by getColumnType(int c).
hope this clarifies...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivor Horton's chapter of his Beginning Java book, on JDBC is on the web, I think at the Sun site. He explains this very well. It's in PDF, or I would paste an excerpt here.
"The Types class in the java.sql package defines public fields of type int that identify the SQL types, and the names of these class data memebers are the same as the SQL types they represent -- such as CHAR,VARCHAR, DOUBLE..."

------------------
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm really confused.. like does it return int or Types object?
 
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

This is an old thread, so things might have changed. The old method is still there, however. As people said before, each type in your database has an INT associated with it, and that is what the method returns. It also tells you to see Types, which tells you what the different numbers returned mean, and the “Constant Field Values” links tell you what each number is. Note the “Since” numbers, which tell you that the metadata interface dates back to JDK1.2 and the Types class to JDK1.1.
If it is a metadata interface, do you actually need to use it?

Moving to our databases forum.
 
Payal Rathee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,
Thanks for replying ☺
Indeed it's an old one.
Actually it says in the docs that the method returns:
SQL type from java.sql.Type
But the declaration says it returns an integer value.  
So was just confused whether it is returning the Types object or an integer.
Now it's clear it is returning an int which can be mapped to a particular datatype with the help of Types class.
Correct me if I'm wrong.
I'm not sure about its usage but this interface might come in handy in case we need some info about the resultset returned by the database like column count or datatypes of the attributes.
 
Campbell Ritchie
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Payal Rathee wrote:. . . an int which can be mapped to a particular datatype with the help of Types class. . . . .

Yes, that appears correct.
 
Rancher
Posts: 326
14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The meta data are handy if you write some sort of database manager like phpmyadmin. Otherwise when design an application both the client and the database should use fixed layouts so the client knows what to expect. Simple speaking the meta data stuff in jdbc is similar to reflections for java itself: You don't need it for anything else other than inspecting stuff with code during runtime rather than lookibg at source or the original design plan.
 
Saloon Keeper
Posts: 9741
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed.

I used metadata for a project that was a code generator. You'd first build your database and then you'd pass that to the generator that, using "show" and "describe", would generate a complete set of Java source code for interacting with it. It was a cool project and I've used it to generate code for several other projects.

All the Types had a 1-1 relationship to Java types except one, and that was "Date". In Java that could be an "sql.Date", a "LocalDateTime", or a "Duration".
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I'm not an expert on this topic but I think the getColumnType() method of the ResultSetMetaData (interface) retrieves the type of the specified column in the current ResultSet object. This method accepts an integer value representing the index of a column and, returns an integer value representing the SQL type of the specified column.
I hope this will help.
 
Campbell Ritchie
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FO, welcome to the Ranch (again; you had a welcome on your earlier post). Yes, I think you are correct there.
 
The only cure for that is hours of television radiation. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic