时间:2021-07-01 10:21:17 帮助过:3人阅读
public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
# public static final String DBURL = "jdbc:oracle:thin:@127.0.0.1:1521/testdb1"; //sid 格式 testdb1是sid
# public static final String DBURL = "jdbc:oracle:thin:@//127.0.0.1:1521/TDB"; //servicename TDB是service_name
public static final String DBUSER = "test\";
public static final String DBPASSWORD = \"xxx\";
public static final String DBURL = \"jdbc:oracle:thin:@TESTDB\"; //tnsname 格式
public static void main(String[] args) throws Exception
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String strSQL = \"select count(*) from tsa_dim_deal\";
# System.setProperty(\"oracle.net.tns_admin\", \"/home/admin/oracle\");//使用tnsname 方法的时候 需要制定tnsname.ora所在的文件夹绝对路径
Class.forName(DBDRIVER).newInstance();
con = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
ps = con.prepareStatement(strSQL);
rs = ps.executeQuery();
while(rs.next())
{
System.out.println(\"num:\"+rs.getString(1));
}
rs.close();
ps.close();
con.close();
}
}
附录:
[qilong.yangql@rac1 oracle]$ more tnsnames.oratestdb=(DESCRIPTION =(FAILOVER = ON)
(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.1)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.2)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.3)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.4)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.5)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.6)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = tdb)))
三 参考:
http://razorsql.com/docs/help_oracle.html
jdbc/ojdbc 链接oracle的三种方式(转)
标签: