Oracle JDBC 连接

司机:

注意: Maven Central 中不包含驱动程序!)

驱动类初始化:

    Class.forName("oracle.jdbc.driver.OracleDriver");

连接 URL

较旧的格式,带有 SID

"jdbc:oracle:thin:@<hostname>:<port>:<SID>"

较新的格式,带有服务名称

"jdbc:oracle:thin:@//<hostname>:<port>/<servicename>"

Tnsnames 喜欢进入

"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=<hostname>)(PORT=<port>))"
    +"(CONNECT_DATA=(SERVICE_NAME=<servicename>)))"

用于故障转移的 RAC 集群连接字符串

"jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(FAILOVER=ON)"
    +"(ADDRESS=(PROTOCOL=TCP)(HOST=<hostname1>)(PORT=<port1>))"
    +"(ADDRESS=(PROTOCOL=TCP)(HOST=<hostname2>)(PORT=<port2>)))"
    +"(CONNECT_DATA=SERVICE_NAME=<servicename>)(SERVER=DEDICATED)))"

connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "HR", "HRPASS");