使用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();
}
}
}
}
总结
以上所述是www.887551.com给大家介绍的使用jdbc连接oracle的三种url格式,希望对大家有所帮助