/*
Przygotowanie bazy danych:
 
drop table osoba;
create table osoba ( id int primary key, imie varchar(20), nazwisko varchar(20), data_ur datetime, pensja decimal(10, 2) )

select * from osoba;
*/

import java.sql.*;

public class Metadane {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws ClassNotFoundException
	{
		Connection connection=null;
		Statement stmt=null;
		try
		{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=KursBD;user=sa;password=sql2005");
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery( "select * from osoba" );
			DatabaseMetaData dm = connection.getMetaData();
	
			System.out.println( dm.getDatabaseProductName()+" : "+dm.getDatabaseProductVersion() );
			System.out.println( dm.getDriverName()+" : "+dm.getDriverVersion()+" : "+dm.getMaxColumnNameLength() );
			System.out.println( "Użytkownik: "+dm.getUserName() );
			
			ResultSetMetaData meta = rs.getMetaData();
			System.out.print( "Lista kolumn: " );
			int col = meta.getColumnCount();
			for( int i = 0; i<col; i++ )
			{
				//System.out.println( meta.getColumnTypeName( ( i+1 ) ) );
				System.out.print( meta.getColumnLabel( (i+1) )+" : "+meta.getColumnTypeName( ( i+1 ) )+"; " );
			}
			System.out.println();
			rs.close();
			stmt.close();
			
			//PreparedStatement pstmt = connection.prepareStatement( "insert into osoba values( ?, ?, ?, ?, ? )" );
			//ParameterMetaData params = pstmt.getParameterMetaData();
			//int ile=params.getParameterCount();

			//System.out.println( "Ilość: "+ile );
			// Niestety większość pozostałych metod nie jest obsługiwana przez ten sterownik
			//pstmt.close();
		}
		catch (SQLException sqle)
		{ 
			sqle.printStackTrace();
			while (sqle != null)
			{
				String logMessage = "\n SQL Error: "+
					sqle.getMessage() + "\n\t\t"+
					"Error code: "+sqle.getErrorCode() + "\n\t\t"+
					"SQLState: "+sqle.getSQLState()+"\n";
				System.out.println(logMessage);
				sqle = sqle.getNextException();
			}
		}
		finally
		{
			try { if ( connection != null ) connection.close(); }
			catch ( Exception e ) { e.printStackTrace(); }
		}
	}
}