> 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/get-connection.md).

# 연결 생성

## 데이터베이스 연결 생성

데이터베이스에 SQL 명령을 전송하기 위해서는 로그인 / 로그아웃 작업이 필요하다. 사용하는 코드는 다음과 같다.

```java
//로그인
String url = "데이터베이스 접속주소";
String username = "데이터베이스 사용자명";
String password = "데이터베이스 사용자 비밀번호";
Connection con = DriverManager.getConnection(url, username, password);

//로그아웃
con.close();
```

### Database URL

Oracle 11g XE 기준 데이터베이스 접속 기본 주소는 다음과 같다.

```java
String url = "jdbc:oracle:thin:@localhost:1521:xe";
```

위 주소는 다음과 같은 항목으로 구성되어 있다.

* `jdbc:oracle:thin:` - 드라이버 타입
* `@` - 구분기호
* `localhost` - 데이터베이스 IP
* `1521` - 접속 Port(Listener Port)
* `xe` - SID 또는 Service Name

드라이버 유형, 데이터베이스의 위치, 접속포트, 서비스 이름에 따라 URL은 달라질 수 있다.

### Database URL 예시

> 사례 1 :&#x20;
>
> * Driver Type : OCI
> * Host : localhost(127.0.0.1)
> * Listener : 1521
> * SID : XE
> * Service Name : XE2

```java
jdbc:oracle:oci:@localhost:1521:xe

jdbc:oracle:oci:@127.0.0.1:1521:xe

jdbc:oracle:oci:@localhost:1521/xe2

jdbc:oracle:oci:@127.0.0.1:1521/xe2
```

> 사례 2 :&#x20;
>
> * Driver Type : THIN
> * Host : localhost(127.0.0.1)
> * Listener : 1521
> * SID : XE
> * Service Name : XE2

```java
jdbc:oracle:thin:@localhost:1521:xe

jdbc:oracle:thin:@127.0.0.1:1521:xe

jdbc:oracle:thin:@localhost:1521/xe2

jdbc:oracle:thin:@127.0.0.1:1521/xe2
```
