Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

시다바리

[Java] Java로 오라클 접속하는 예제 본문

Language/Java

[Java] Java로 오라클 접속하는 예제

호서아빠 2008. 8. 8. 12:04


/**
 * Java로 오라클 접속하는 예제
 * Myoung-Geun Jang(jangsalt@nextelecom.co.kr)
 * 2008. 5. 9
 */

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

public class OracleConnection {
 private static Driver JDBC_DRIVER = null;

 Connection connection = null;
 ResultSet rs = null;
 Statement stmt = null;

 String strSql = String.format("select * from (Table_Name)");

 static {
  loadDriver();
 }

 private static void loadDriver() {
  try {
   JDBC_DRIVER = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
   System.out.println("드라이버 로딩 성공...");
  } catch(Exception e) {
   System.err.println(e.getMessage());
   e.printStackTrace();
  }
 }

 public OracleConnection() {
  System.out.println("Oracle DB 접속 중 ...");
 
  String url = "jdbc:oracle:thin:@(Oracle IP):(Oracle Port):(SID)";
  String user = "(ID)";
  String pwd = "(Password)";

  int test;
       
  try {
   connection = DriverManager.getConnection( url, user, pwd );
   System.out.println("DB 연결 성공!");
  } catch(Exception e) {
   System.err.println(e.getMessage());
  }

  try {
   stmt = connection.createStatement();

   test = stmt.executeUpdate(insSql);

   rs = stmt.executeQuery(strSql);
   
   while(rs.next()){
    System.out.println(rs.getString(1));
   }
   rs.close();

  } catch( SQLException e ) {
   System.err.println( "SQL Exception : " + e.getMessage() );
  } finally {
   if (stmt != null) {
    try {
     stmt.close();
    } catch (SQLException e) {
     System.err.println(e.getMessage());
    }
   }
  }
 }
 
 public static void main(String[] args) {
  OracleConnection oc = new OracleConnection();
 }
}

Comments