'순서'에 해당되는 글 1건

  1. 2009.01.15 [DataBase] JDBC 작성 순서

[DataBase] JDBC 작성 순서

DB 2009. 1. 15. 11:59
1. JDBC드라이버 로딩
오라클 - oracle.jdbc.driver.OracleDriver

  try{
  Class.forName("oracle.jdbc.driver.OracleDriver");//대소문자 주의
  }catch(ClassNotFoundException cnfe){
   cnfe.printStackTrace();
  }

2. 데이터베이스 커넥션 구함

 public Connection getConnection(){
  Connection con=null;
  String dburl = "jdbc:oracle:thin:@localhost:1521:orcl";//127.0.0.1루프백, 도메인, 로컬호스트
  String dboid = "scott";
  String dbopass = "tiger";
    
  try{
  con = DriverManager.getConnection(dburl,dboid,dbopass);
  }
  catch(SQLException e){
   e.printStackTrace();
  }//end catch
  
  return con;
 }
//singletonpattern이용

3. 쿼리 실행을 위한 statement 객체 생성

Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

   con = SingletonConnection.getInstance().getConnection();
   StringBuffer selectQuery = new StringBuffer();
   selectQuery.append("select num, name, address, phone, age from classinfo");
   pstmt = con.prepareStatement(selectQuery.toString());
  
4. 쿼리 실행

 rs = pstmt.executeQuery();

5. 쿼리 실행 결과 사용

 while(rs.next()){
    //조회된 결과를 자바빈즈로 생성
    xmlDateDTO xdd = new xmlDateDTO(
            rs.getInt("num"),
            rs.getInt("age"),
            rs.getString("name"),
            rs.getString("address"),
            rs.getString("phone"));//빈즈를 list로 관리
    classList.add(xdd);
   }

6. statement 종료

if(rs!=null){rs.close();}
if(pstmt!=null){pstmt.close();}

7. 데이터베이스 커넥션 종료

if(con!=null){con.close();}

Posted by zeide
,