java中如何使用JDBC连接TNS Oracle呢?
下文笔者讲述jdbc连接tns oracle数据库的方法分享,如下所示
实现思路: 1.设置属性,并指向tnsnames.ora文件所在的文件夹 2.编写相应的业务逻辑,即可实现oracle的连接例:
import java.sql.*; public class Main { public static void main(String[] args) throws Exception { System.setProperty("oracle.net.tns_admin", "D:/test/client_1/ADMIN"); String dbURL = "jdbc:oracle:thin:@ENTRY_FROM_TNSNAMES"; Class.forName ("oracle.jdbc.OracleDriver"); Connection conn = null; Statement stmt = null; try { conn = DriverManager.getConnection(dbURL, "user_name", "password"); stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual"); if (rs.next()) { System.out.println(rs.getString(1)); } } catch (Exception e) { e.printStackTrace(); } finally { if (stmt != null) try { stmt.close(); } catch (Exception e) {} if (conn != null) try { conn.close(); } catch (Exception e) {} } } }
tnsnames.ora内容
my_net_service_name = (DESCRIPTION = (地址=(此处为一些地址)) (CONNECT_DATA = (SID = some_SID_name)))
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。