1. 程式人生 > >JDBC連線ORACLE的三種URL格式

JDBC連線ORACLE的三種URL格式

使用jdbc連線oracle時url有三種格式

格式一: Oracle JDBC Thin using an SID:
jdbc:oracle:thin:@host:port:SID
Example: jdbc:oracle:thin:@localhost:1521:orcl
這種格式是最簡單也是用得最多的
你的oracle的sid可以通過一下指令獲得:
sqlplus / as sysdba
select value from v$parameter where name=’instance_name’;

import java.sql.*;

public class
TestOrclConnect { public static void main(String[] args) { ResultSet rs = null; Statement stmt = null; Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String dbURL = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(dbURL, "admin2"
, "123"); System.out.println("連線成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null
; } if (stmt != null) { stmt.close(); stmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } } }

格式二: Oracle JDBC Thin using a ServiceName:
jdbc:oracle:thin:@//host:port/service_name
Example:jdbc:oracle:thin:@//localhost:1521/orcl.city.com
注意這裡的格式,@後面有//, port後面:換成了/,這種格式是Oracle 推薦的格式,因為對於叢集來說,每個節點的SID 是不一樣的,但是SERVICE_NAME 確可以包含所有節點。
你的oracle的service_name可以通過以下方式獲得:
sqlplus / as sysdba
select value from v$parameter where name=’service_names’;

import java.sql.*;

public class TestOrclConnect {

    public static void main(String[] args) {
        ResultSet rs = null;
        Statement stmt = null;
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String dbURL = "jdbc:oracle:thin:@//localhost:1521/orcl.city.com";
            conn = DriverManager.getConnection(dbURL, "admin2", "123");
            System.out.println("連線成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

格式三:Oracle JDBC Thin using a TNSName:
jdbc:oracle:thin:@TNSName
Example: jdbc:oracle:thin:@TNS_ALIAS_NAME
我在谷歌上找了一些資源,要實現這種連線方式首先要建立tnsnames.ora檔案,然後通過System.setProperty指明這個檔案路徑。再通過上面URL中的@符號指定檔案中的要使用到的資源。
這種格式我現在水平幾乎沒見過,對於我來說用得到這種的情況並不多吧。當然既然是通過配置檔案來讀取指定資源肯定也可以直接將資源拿出來放在URL中,直接放在URL中的URL模版是下面這樣的(tnsnames.ora這個檔案中放的就是@符號後面的那一段程式碼,當然用檔案的好處就是可以配置多個,便於管理):

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP)(HOST=hostA)(PORT= 1522))(ADDRESS=(PROTOCOL=TCP)(HOST=your host)(PORT=1521)))(SOURCE_ROUTE=yes)(CONNECT_DATA=(SERVICE_NAME=your service_name)))

jdbc連線程式碼如下:

“`
import java.sql.*;

public class TestOrclConnect {

public static void main(String[] args) {
    ResultSet rs = null;
    Statement stmt = null;
    Connection conn = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
         String dbURL =
        "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))"
        + "(CONNECT_DATA=(SERVICE_NAME=orcl.city.com)))";
        conn = DriverManager.getConnection(dbURL, "admin2", "123");
        System.out.println("連線成功");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (conn != null) {
                conn.close();
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

}
轉載請註明出處