> For the complete documentation index, see [llms.txt](https://docs.sysout.co.kr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sysout.co.kr/base-language/java/jdbc/driver-load.md).

# 드라이버 로드

## 드라이버 로드

데이터베이스에 연결하기 위해서는 드라이버 로드 작업이 필요하다. 코드로는 다음과 같다.

```java
Class.forName("oracle.jdbc.OracleDriver");
```

### Class.forName()

{% hint style="info" %}
공식 API에는 다음과 같이 기술되어 있다.&#x20;

*Returns the `Class` object associated with the class or interface with the given string name.*

*주어진 문자열 이름을 가진 클래스 또는 인터페이스와 관련된 Class 객체를 반환합니다*.
{% endhint %}

Class.forName()을 이용하여 클래스 정보를 획득한 뒤 Java Reflection을 이용하여 객체 생성이나 필드 메소드 분석 등을 수행할 수 있도록 연계할 수 있다.

{% tabs %}
{% tab title="ExampleObject" %}
{% code title="ExampleObject.java" %}

```java
package test;

public class ExampleObject {
	
	static {
		System.out.println("static 실행");
	}
	
	public ExampleObject() {
		System.out.println("생성자 실행");
	}
	
}

```

{% endcode %}
{% endtab %}

{% tab title="ExampleApplication" %}
{% code title="ExampleApplication.java" %}

```java
package test;

import java.lang.reflect.InvocationTargetException;

public class ExampleApplication {
	public static void main(String[] args) throws Exception {
		Object obj = Class.forName("test.ExampleObject");
		Class<ExampleObject> clazz = (Class<ExampleObject>) obj;
		ExampleObject ex = clazz.getDeclaredConstructor().newInstance();
		//ExampleObject ex = new ExampleObject();
	}
}

```

{% endcode %}
{% endtab %}

{% tab title="결과" %}

```
static 실행
생성자 실행
```

{% endtab %}
{% endtabs %}

즉 Class.forName() 메소드 클래스 정보를 로드하는 역할을 수행하며, 이 과정에서 static 데이터가 메모리상에 적재되는 것을 확인할 수 있다.

{% tabs %}
{% tab title="ExampleObject" %}
{% code title="ExampleObject.java" %}

```java
package test;

public class ExampleObject {
	
	static {
		System.out.println("static 실행");
	}
	
	public ExampleObject() {
		System.out.println("생성자 실행");
	}
	
}

```

{% endcode %}
{% endtab %}

{% tab title="ExampleApplication" %}
{% code title="ExampleApplication.java" %}

```java
package test;

import java.lang.reflect.InvocationTargetException;

public class ExampleApplication {
	public static void main(String[] args) throws Exception {
		Class.forName("test.ExampleObject");
	}
}

```

{% endcode %}
{% endtab %}

{% tab title="결과" %}

```
static 실행
```

{% endtab %}
{% endtabs %}

따라서 `Class.forName("oracle.jdbc.OracleDriver")`는 `ojdbc.jar`의 `OracleDriver` 클래스를 호출하여 static 요소들을 메모리에 등록하기 위한 코드라고 볼 수 있다.

{% hint style="info" %}
공식 문서에 따르면 oracle.jdbc.driver.OracleDriver는 사용을 권장하지 않는다.

<https://docs.oracle.com/cd/E18283_01/appdev.112/e13995/oracle/jdbc/package-summary.html>

&#x20;*Your code should use the package `oracle.jdbc` instead of the package `oracle.jdbc.driver` used in earlier versions of Oracle. Use of the package `oracle.jdbc.driver` is now deprecated, but will continue to be supported for backwards compatibility.*

코드는 이전 버전의 Oracle에서 사용된 oracle.jdbc.driver 패키지 대신 oracle.jdbc 패키지를 사용해야 합니다. oracle.jdbc.driver 패키지의 사용은 이제 더 이상 사용되지 않지만 이전 버전과의 호환성을 위해 계속 지원됩니다.
{% endhint %}
