Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java program to get data type of column field 
 

In this example java program we have to get the data type of the database table fields.

 

Java program to get data type of column field

                         

In this example java program we have to get the data type of the database table fields. For this purpose we have established database connection via JDBC and after connecting database we have get the data tables meta data for getting the data type of the table columns. Here we have used MySQL database for connection with the data table. Table structure for webpages table of database "any" is as follows:

 

 

 

 

ResultSet rs = st.executeQuery("SELECT * FROM webpages"); 
ResultSetMetaData rsmd = rs.getMetaData();

Above line of code gets ResultSetMetaData object which will be further used for getting related information. We can use the getColumnCount() methods for getting the column index and these index will be used for getting column type and with the getColumnTypeName(index) method of ResultSetMetaData class.

Here is the example code of GetColumnDataType.java as follows:

 GetColumnDataType.java

import java.sql.*;
public class GetColumnDataType {
  public static void main(String[] args) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/";
    String username = "root";
    String password = "root";
    String dbName= "any";
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(
                      url+dbName,
                      username,
                      password);
    System.out.println("Connected");
    Statement st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM webpages");
    ResultSetMetaData rsmd = rs.getMetaData();
    int NumOfCol = rsmd.getColumnCount();
    for(int i=1;i<=NumOfCol;i++)
    {
    System.out.println("Name of ["+i+"] Column data type is ="
         +rsmd.getColumnTypeName(i));
    }
    st.close();
    conn.close();
  }

Output:


C:\javaexamples>javac GetColumnDataType.java

C:\javaexamples>java GetColumnDataType
Connected
Name of [1] Column data type is =DOUBLE
Name of [2] Column data type is =VARCHAR
Name of [3] Column data type is =VARCHAR
Name of [4] Column data type is =BLOB

Download Source Code

 

                         

» View all related tutorials
Related Tags: java c mac process io virtual vi number this sso ai processor to proc e il section machine ce in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.