JAVA DATABASE CONNECTIVITY 자바와 데이터베이스 사이 연결다리 역할을 해주는 "자바 API"
역사[편집]
썬 마이크로시스템즈는 1997년 2월 19일 JDBC를 JDK 1.1의 일부로 출시하였다.[1] 그 뒤로 이제까지 자바 SE의 일부로 되고 있다.
JDBC 클래스는 자바 패키지 java.sql과 javax.sql에 포함되어 있다.
JDBC 드라이버[편집]
이 부분의 본문은 JDBC 드라이버입니다.
JDBC 드라이버들은 자바 프로그램의 요청을 DBMS가 이해할 수 있는 프로토콜로 변환해주는 클라이언트 사이드 어댑터이다. (서버가 아닌 클라이언트 머신에 설치)
자바 애플리케이션이 데이터베이스 연결이 필요할 때 DriverManager.getConnection() 메소드들 가운데 하나를 사용하여 JDBC 연결을 만들게 된다. 사용된 URL은 특정 데이터베이스와 JDBC 드라이버에 의존한다. "jdbc:" 프로토콜로 무조건 시작하지만 나머지 부분은 특정 벤더에 따라 다르다.
Connection conn = DriverManager.getConnection(
"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword" );
try { /* you use the connection here */
} finally {
//It's important to close the connection when you are done with it
try { conn.close(); } catch (Throwable e) { /* Propagate the original exception
instead of this one that you want just logged */ logger.warn("Could not close JDBC Connection",e); }
}
자바 SE 7을 기점으로 자바의 try-with-resources 문을 사용하여 위의 코드를 더 깔끔하게 정리할 수 있다:
try (Connection conn = DriverManager.getConnection(
"jdbc:somejdbcvendor:other data needed by some jdbc vendor",
"myLogin",
"myPassword" ) ) {
/* you use the connection here */
} // the VM will take care of closing the connection
연결이 확립되면 다음과 같은 문을 작성할 수 있다.
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate( "INSERT INTO MyTable( name ) VALUES ( 'my name' ) " );
}
'컴퓨터 지식' 카테고리의 다른 글
CLI (0) | 2021.10.15 |
---|---|
프로토콜 (0) | 2021.10.08 |
Permgen space (0) | 2021.10.06 |
parameter && argument (0) | 2021.10.06 |
eclipse.ini && -vm (0) | 2021.10.06 |