Jam's story

[JDBC] days06- ResultSetMetaData 본문

JDBC

[JDBC] days06- ResultSetMetaData

애플쩀 2022. 5. 11. 12:45
테이블 이름 출력
번호붙이기 추가 (오라클에서 rownum싸도됨 ) 

 

ResultSetMetaData
rs의 정보를 얻어오는 메소드
ResultSet으로부터 어떤 칼럼, 칼럼자료형 ,칼럼 갯수 등등 정보를 얻어와서 사용하는 기술을 리플렉션


내가 생각하는 중요한 부분 

if(columnType==Types.NUMERIC && scale==0) {//정수
					System.out.printf("%d\t", rs.getInt(i));
				} else if(columnType==Types.NUMERIC && scale!=0) {
					System.out.printf("%.2f\t", rs.getDouble(i));
				}else if (columnType == Types.VARCHAR || columnType==Types.CLOB) {//문자
					System.out.printf("%s\t ".concat(rs.getString(1)));
				}else if(columnType==Types.DATE || columnType==Types.TIMESTAMP) {//날짜
					System.out.printf("%tF\t", rs.getDate(i));
				}
package days06;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Iterator;
import java.util.Scanner;

import com.util.DBconn;

public class Prac {

	public static void main(String[] args) {

		String sql="select table_name from tabs order by table_name asc";
		Connection con=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		con=DBconn.getConnection();
		try {
			pstmt=con.prepareStatement(sql);
			rs=pstmt.executeQuery();
			int no=1;
			while(rs.next()) {
				System.out.printf("%d  : %s\n",no++, rs.getString(1));
			}
			
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
		try {
			pstmt.close();
			rs.close();
		} catch (SQLException e2) {
			// TODO: handle exception
			e2.printStackTrace();
		}
		}
		
		System.out.println("테이블을 선택하세요 ");
		Scanner sc=new Scanner(System.in);
		String tableName=sc.next();
		sql="select * from "+tableName;
		 
		try {
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			//rs 객체로부터 정보 (컬럼개수, 컬럼명, 컬럼자료형 등등) 추출
			ResultSetMetaData rsmd=  rs.getMetaData(); //rs의 정보를 얻어오는 메소드
			System.out.println("> 컬럼 개수 : "+rsmd.getColumnCount());

			System.out.println("--------------------------------------------------------------------------------------------");
			for (int i =1; i <= rsmd.getColumnCount(); i++) {
				System.out.printf("%s\t", rsmd.getColumnName(i));
			}
			System.out.println();
			System.out.println("--------------------------------------------------------------------------------------------");
			
			while (rs.next()){
			for (int i = 1; i < rsmd.getColumnCount(); i++) {
				int scale=rsmd.getScale(i);// Number(3,2) 여기서 2가 scale
				int columnType=rsmd.getColumnType(i);
				if(columnType==Types.NUMERIC && scale==0) {//정수
					System.out.printf("%d\t", rs.getInt(i));
				} else if(columnType==Types.NUMERIC && scale!=0) {
					System.out.printf("%.2f\t", rs.getDouble(i));
				}else if (columnType == Types.VARCHAR || columnType==Types.CLOB) {//문자
					System.out.printf("%s\t ".concat(rs.getString(1)));
				}else if(columnType==Types.DATE || columnType==Types.TIMESTAMP) {//날짜
					System.out.printf("%tF\t", rs.getDate(i));
				}
			}
				
			}
		 
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {

			try {
				pstmt.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
		}
	}}

 

 

에러!!! 
java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name
쿼리가 잘못되었을 때 에러 

▶테이블명, 칼럼명은  바인딩변수  ? 를 사용할 수 없다.◀ 


sql="select * from ? "; 를 바꿔야한다. 
-> 동적쿼리를 사용하거나 

-> sql="select * from"+ tableName;

 

Comments