연결 설정

DriverManager는 JDBC 드라이버를 관리하기 위한 기본적인 인터페이스이며, 데이터베이스 드라이버를 선택하고 새로운 데이터베이스 연결을 생성하는 기능을 한다. CUBRID JDBC 드라이버가 등록되어 있다면 DriverManager.getConnection(db-url, user-id, password) 메소드를 호출하여 데이터베이스에 접속한다. getConnection 메소드는 Connection 객체를 반환한다. 그리고 그것은 질의 실행과 명령문 실행 그리고 트랜잭션의 커밋 또는 롤백에 사용된다. 연결 설정을 위한 db-url 인자의 구성은 다음과 같다.

jdbc:cubrid:<host>:<port>:<db-name>:[user-id]:[password]:[?<property> [& <property>]]

 

<host> ::=

hostname | ip_address

 

<property> ::=

althosts= <alternative_hosts> | rctime= <second> | charset= <character_set>

 

<alternative_hosts> :

<standby_broker1_host>:<port> [,<standby_broker2_host>:<port>]

예제 1

--connection URL string when user name and password omitted

 

URL=jdbc:CUBRID:127.0.0.1:31000:db1:::

 

--connection URL string when charset property specified

 

URL=jdbc:CUBRID:127.0.0.1:31000:db1:::?charset=utf-8

 

--connection URL string when a property(althosts) specified for HA

URL=jdbc:CUBRID:127.0.0.1:31000:db1:::?althosts=127.0.0.2:31000,127.0.0.3:31000

 

--connection URL string when properties(althosts,rctime) specified for HA

URL=jdbc:CUBRID:127.0.0.1:31000:db1:::?althosts=127.0.0.2:31000,127.0.0.3:31000&rctime=600

 

--connection URL string when properties(althosts,rctime, charset) specified for HA

URL=jdbc:CUBRID:127.0.0.1:31000:db1:::?althosts=127.0.0.2:31000,127.0.0.3:31000&rctime=600&charset=utf-8

예제 2

String url = "jdbc:cubrid:127.0.0.1:33000:demodb:::";

String userid = "";

String password = "";

 

try {

   Connection conn =

           DriverManager.getConnection(url,userid,password);

   // Do something with the Connection

 

   ...

 

   } catch (SQLException e) {

       System.out.println("SQLException:" + e.getMessage());

       System.out.println("SQLState: " + e.getSQLState());

   }

   ...

참고 트랜잭션 롤백 요청을 하는 rollback 메소드는 서버가 롤백 작업을 완료한 후 종료된다.