package JDBC_17;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.pool.OracleDataSource;

/*
 * 이방법은 먼저 constuctor에서 setURL 정의
 * Connection con = ds.getConnection("scott", "tiger");
 Statement stmt = con.createStatement();
 stmt.execute("select * from dept");
 */
public class TestDS {
 
 //다른 클래스에서 참조할수 있도록 멤베변수화.
 
 OracleDataSource ds;
 
 public TestDS() {
   try {
    ds = new OracleDataSource();
    ds.setURL("jdbc:oracle:thin:@211.234.53.68:1521:orcl");
   
   } catch (SQLException e) {
    e.printStackTrace();
    System.out.println("Error 났쎄요...");
   }
 }

 private void execute() {
  // Member 변수는 자동으로 초기값이 되지만.
  //지역변수는 초기화가 안되용~
    ResultSet  rs=null;
    Statement  stmt=null;
    Connection con=null;
    try {
     con =
        ds.getConnection("scott", "tiger");
     stmt = con.createStatement();
     rs = stmt.executeQuery("select * from dept");
     while(rs.next()){
       System.out.println(
         rs.getString(1)+","+
         rs.getString(2)+","+
         rs.getString(3)+",");
     }
     
     //System.out.printlf(%10,%5,%8,%5, );
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    finally{
      try {
       rs.close();
       stmt.close();
       con.close();
      } catch (SQLException e) {
       e.printStackTrace();
      }
    }
 }
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
   new TestDS().execute();
 }
 

}

다른 카테고리의 글 목록

웹 개발/JDBC 기초 카테고리의 포스트를 톺아봅니다