Jam's story
[JDBC] days06- ResultSetMetaData 본문
테이블 이름 출력
번호붙이기 추가 (오라클에서 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;
'JDBC' 카테고리의 다른 글
[JDBC]days05- CallableStatement , 자바에서 커서 이용 (0) | 2022.05.11 |
---|---|
[JDBC] days05- CollableStatement (0) | 2022.05.10 |
[JDBC] 페이징 처리 (0) | 2022.05.10 |
[JDBC]days04 - 게시판 만들기 (DTO ,DAO) (0) | 2022.05.09 |
[JDBC] days02- (조회,수정,삭제,검색) (0) | 2022.05.04 |
Comments