Friday, June 30, 2006

[java] Gets metadata of the resultSet (SQL)

Gets metadata of the resultSet (SQL)

import java.sql.*;

import java.util.*;

import java.io.*;

public class hed

{

public static void main(String args[])

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:test");

System.out.println("Connection established");

DatabaseMetaData rdata = con.getMetaData();

System.out.println("Database name: "+rdata.getDatabaseProductName());

System.out.println("Product version: "+rdata.getDatabaseProductVersion());

System.out.println("User name: "+rdata.getUserName());

Statement stat = con.createStatement();

ResultSet r = stat.executeQuery("select * from authors");

ResultSetMetaData rmeta = r.getMetaData();

int h = rmeta.getColumnCount();

for (int i=1; i<=h; ++i)

{

if(i<h)

{

System.out.print(rmeta.getColumnName(i) + " | ");

}

else

System.out.println();

}

while(r.next())

{

for (int i=1; i<=h; ++i)

{

if (i<h)

{

System.out.print(r.getString(i) + " | ");

}

else

{

System.out.println();

}

}

}

}

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India

No comments: