JAVA & Open Framework2011. 12. 19. 17:51

//java로 oracleDB접속 하는 방법?
import java.sql.*; //JDBC API가 있는 package
class DBtest
{
 public static void main(String[] args)
 {
 try{
  //[0].Oracle 접속 Draver를 Loading
  Class.forName("oracle.jdbc.driver.OracleDriver");
  //[1].Connection 객체를 생성
  //C:\Program Files\Java\jdk1.6.0_04\jre\lib\ext
  String conStr = "jdbc:oracle:thin:@127.0.0.1:1521:ORA92";
  String user ="scott";
  String pwd = "tiger";
  Connection objCon=DriverManager.getConnection(conStr,user,pwd);
  if(objCon!=null)
  {
   String strSQL = "select * from japan";
   Statement stmt = objCon.createStatement(); //SqlCommand
   ResultSet rs = stmt.executeQuery(strSQL); //SqlDataReader
   while(rs.next())
   {
    System.out.println(rs.getString("id")+"\t"+rs.getString("name"));
   }
   System.out.println("접속성공");
  }else
  {
   System.out.println("접속실패");
  }
 }catch(Exception e){
  e.printStackTrace();
 }
 }
}




-----------------------------------------------------------------------------------------

create japan (
 id varchar2(10),
name varchar2(10)
);


출처 : http://windjasi.tistory.com/86

Posted by 아로나