jdbc 데이터베이스 열 이름 가 져 오기

package org.data.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;


public class Jdbc {

	//       
	private static final String url="jdbc:mysql://localhost:3306/data";
	//     
	private static final String user="root";
	private static final String pwd="123456";

	/***
	 *      
	 * @throws ClassNotFoundException 
	 */
	public static void query() throws ClassNotFoundException{
		Connection conn=null;
		java.sql.PreparedStatement pst=null;
		ResultSet re=null;
		/*
		 * DBC   DatabaseMetaData                ,  Connection getMetaData        。
		 * DatabaseMetaData                ,                 。
		 *                  ,              。
		 */
		 DatabaseMetaData metadata=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url, user, pwd);		
			
			metadata = conn.getMetaData();
			System.out.println("        : "+ metadata.getUserName());   
	         System.out.println("               : "+ metadata.getSystemFunctions());   
	         System.out.println("                  : "+ metadata.getTimeDateFunctions());   
	         System.out.println("                : "+ metadata.getStringFunctions());   
	         System.out.println("         'schema'      : "+ metadata.getSchemaTerm());   
	         System.out.println("   URL: " + metadata.getURL());   
	         System.out.println("      :" + metadata.isReadOnly());   
	         System.out.println("        :" + metadata.getDatabaseProductName());   
	         System.out.println("      :" + metadata.getDatabaseProductVersion());   
	         System.out.println("       :" + metadata.getDriverName());   
	         System.out.println("       :" + metadata.getDriverVersion());   
	         System.out.println("       :" + metadata.getTableTypes());  
	         System.out.println("............................................");
	         pst= conn.prepareStatement("select * from customerinfo");
	         
	         //      
	         re=pst.executeQuery();
	         //     
	         ResultSetMetaData data= re.getMetaData();
	         //getColumnCount       
	         for(int i=1;i<=data.getColumnCount();i++){
	        	 //     index  1  、  、   、      
	        	 System.out.println(data.getColumnName(i)+"\t"+data.getColumnTypeName(i)+"\t"+data.getColumnDisplaySize(i));
	         }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeConnection(conn, pst, re);
		}   
	}
	

	/**
	 *        
	 * @param conn
	 * @param pst
	 * @param re
	 */
	public static void closeConnection(Connection conn,PreparedStatement pst,ResultSet re){
		
		if(conn!=null)
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(pst!=null)
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(re!=null)
			try {
				re.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public static void main(String[] args) {
		try {
			query();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기