MetaData

DB/JDBC : 2007. 4. 4. 20:02
반응형


import java.sql.*;

public class jdbctest {
  String driver_name;
  String URL;
  String uid;
     String passwd;
     String sql;

     Connection con;
     Statement stmt;
     ResultSet rs;
     ResultSetMetaData rsmd;
 
     public jdbctest() {
      //주의 : @//PC_이름:1521/orcl쓸것
   URL="jdbc:oracle:thin:@//localhost:1521/ORCL";
   uid="scott";
   passwd="tiger";
     }  
  public static void main(String[] args){
   jdbctest x = new jdbctest();
   x.DB_Connection();
   x.DB_DisConnection();
  }
  public void DB_Connection() {
         try{
    DriverManager.registerDriver(
      new oracle.jdbc.driver.OracleDriver());
    con = DriverManager.getConnection(URL,uid,passwd);
    sql="SELECT * FROM department where deptno=?";
    PreparedStatement ps = con.prepareStatement(sql);    
    ps.setInt(1,102);
    rs=ps.executeQuery();
    rsmd=rs.getMetaData();
    int columcount=rsmd.getColumnCount();
    System.out.println("DEPT RESULT\n***********************************************");
    System.out.println(" NUM COLUMN: "+columcount);
    System.out.println("------------------------------------------------");    
    for (int q=1;q<=columcount;q++){
     System.out.print(rsmd.getColumnName(q)+"     ");
    }
    System.out.println("");
    while(rs.next()){
     System.out.print(rs.getString(1)+"      ");
     System.out.print(rs.getString(2)+"      ");
     System.out.print(rs.getString(3)+"      ");
     System.out.print(rs.getString(4)+"      ");
    }
         }catch(SQLException e)
         {
             e.printStackTrace();
         }
  }
  public void DB_DisConnection(){
   try{
    con.close();
   }catch(SQLException e){
    e.printStackTrace();
   }
  } 
}

/*--------------------결과 ----------------//
DEPT RESULT
***********************************************
 NUM COLUMN: 4
------------------------------------------------
DEPTNO     DNAME     COLLEGE     LOC    
102      멀티미디어학과      100      2호관 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 

반응형

'DB > JDBC' 카테고리의 다른 글

JDBC로 MySql 접속하기  (0) 2007.06.16
PreparedStatement  (0) 2007.04.04
JDBC 기초  (0) 2007.04.03
Posted by Real_G