MisoBoy Blog...

[20110720] ResultSetMetaData 객체를 사용하여 출력하는 예제 본문

Java

[20110720] ResultSetMetaData 객체를 사용하여 출력하는 예제

misoboy 2011. 7. 20. 14:02

ResultSet 이란 ?
ResultSet 은 excuteQury 이 실행 된 데이터를 담아두는 역활을 합니다.
그러나 ResultSet 에 있는 데이터가 어떠한 구조로 되어 있는 데이터 인지 알수가 없습니다.
그래서 구조를 알수 있게 해주는 ResultSetMetaData 가 있습니다. 

우선 API 를 참조 하겠습니다. 




API 를 참조 하시면 여러 Method 가 있는 것을 확인 할 수 있으며 그중 몇개만 보도록 하겠습니다.
getColumLabel() : Colum 의 Label 을 알고자 할 때 사용
getColumnType() : Colum 의 Type 을 알고자 할때 사용
getColumName() :  Colum 의 Name 을 알고자 할때 사용
getColumnTypeName() : Colum 의 Type Name 을 알고자 할떄 사용
다른 Method 는 API 를 참고 하시면 되겠습니다. 

아래 소스는 ResultSetMetaData 를 사용하여 SQL의 데이터의 Type 별로 출력을 하는 형식 입니다.



import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;


// ResultSetMetaData 객체 연습

public class JDBCDemo {
	public static void main(String [] args) throws IOException, SQLException,
									 ClassNotFoundException{
		// DBBean 클래스를 생성하여 oracle DB를 실행시키자
		DBBean oracle = new DBBean("oracle");				
		// Oracle Driver 실행
		oracle.loadDriver();								
		// DB 접속
		oracle.setConnection();								
		// 접속정보 GET
		Connection conn = oracle.getConnection();			
		// Statement 생성
		Statement stmt = conn.createStatement();			
		// 실행 될 SQL 선언
		String sql = "SELECT * FROM emp";					
		// 문자 열 AS 를 주기위해서는 ' \ ' 를 준다.
		// String sql = "SELECT empno AS \"사번\" from emp"; 
		// ResultSet 에는 데이터만 들어 있어서 구조를 알수 없다.
		// 쿼리의 데이터를 ResultSet 에 담는다.
		ResultSet rs = stmt.executeQuery(sql);			
		// ResultSet의 데이터를 ResultSetMetaData 로 구조를 파악한다.
		ResultSetMetaData rsmd = rs.getMetaData();	
		int count = rsmd.getColumnCount();			// 컬럼의 갯수를 구할 수 있다.
		while(rs.next()){						// 해당 컬럼에 있는 데이터를 출력한다.
			for(int i = 1; i <= count; i++){				// 컬럼의 시작은 1부터 시작이다.
				// Column 의 Label 을 알아내자
				//System.out.println("label = " + rsmd.getColumnLabel(i));				
				// Column 의 Type 을 알아내자
				//System.out.println("ColumnType = " + rsmd.getColumnType(i));			
				// Column 의 Name 을 알아내자
				//System.out.println("name = " + rsmd.getColumnName(i));         		
				// Column Type Name 을 알아내자
				//System.out.println("TypeName = " + rsmd.getColumnTypeName(i));
				
				// TYPE 를 구분하며 출력하기 위해 switch 를 사용한다.
				switch(rsmd.getColumnType(i)){				
				case Types.NUMERIC :											
				case Types.INTEGER :
					System.out.printf("%d\t", rs.getInt(i)); break;
				case Types.FLOAT :
					System.out.printf("%.2f\t", rs.getFloat(i)); break;
				case Types.DOUBLE :
					System.out.printf("%.2f\t", rs.getDouble(i)); break;
				case Types.DATE :
					System.out.printf("%tF\t", rs.getDate(i)); break;
				case Types.CHAR :
				default :
					System.out.printf("%s\t", rs.getString(i));
				}  //switch end
			} //for end
			System.out.println();
		}//while end
		oracle.rsClose();
		oracle.stmtClose();
		oracle.connClose();
	}
}