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> | connectTimeout=<second> | queryTimeout=<second> | charset=<character_set> | zeroDateTimeBehavior=<behavior_type> | logFile=<file_name> | logOnException=<bool_type> | logSlowQueries=<bool_type>&slowQueryThresholdMillis=<millisecond>
<alternative_hosts> ::=
<standby_broker1_host>:<port> [,<standby_broker2_host>:<port>]
<behavior_type> ::= exception | round | convertToNull
<bool_type> ::= true | false
--connection URL string when user name and password omitted
URL=jdbc:CUBRID:192.168.0.1:33000:db1:::
--connection URL string when zeroDateTimeBehavior property specified
URL=jdbc:CUBRID:127.0.0.1:31000:db1:::?zeroDateTimeBehavior=convertToNull
--connection URL string when charset property specified
URL=jdbc:CUBRID:192.168.0.1:33000:db1:::?charset=utf-8
--connection URL string when queryTimeout and charset property specified
URL=jdbc:CUBRID:127.0.0.1:31000:db1:::?queryTimeout=1&charset=utf-8
--connection URL string when a property(althosts) specified for HA
URL=jdbc:CUBRID:192.168.0.1:33000:db1:::?althosts=192.168.0.2:33000,192.168.0.3:33000
--connection URL string when properties(althosts,rctime, connectTimeout) specified for HA
URL=jdbc:CUBRID:192.168.0.1:33000:db1:::?althosts=192.168.0.2:33000,192.168.0.3:33000&rctime=600&connectTimeout=5
--connection URL string when properties(althosts,rctime, charset) specified for HA
URL=jdbc:CUBRID:192.168.0.1:33000:db1:::?althosts=192.168.0.2:33000,192.168.0.3:33000&rctime=600&charset=utf-8
String url = "jdbc:cubrid:192.168.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 메소드는 서버가 롤백 작업을 완료한 후 종료된다.