Listener.ora SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = dave) (ORACLE_HOME =D:\app\Administrator\product\11.2.0\dbhome_1) (SID_NAME = NEWCCS) ) ) Tnsnames.ora DVD = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = dave) ) )
2.2 測試1,使用SID:newccs
import java.sql.*; public class jdbc { String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:newccs"; String theUser = "dave"; String thePw = "dave"; Connection c = null; Statement conn; ResultSet rs = null; public jdbc() { try { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); c = DriverManager.getConnection(dbUrl, theUser, thePw); conn = c.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public boolean executeUpdate(String sql) { try { conn.executeUpdate(sql); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } public ResultSet executeQuery(String sql) { rs = null; try { rs = conn.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public void close() { try { conn.close(); c.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ResultSet rs; jdbc conn = new jdbc(); rs = conn.executeQuery("select * from dave where rownum<5"); try { while (rs.next()) { System.out.println(rs.getString("username")+"--"+rs.getString("user_id")); } } catch (Exception e) { e.printStackTrace(); } } } ---輸出正常: MGMT_VIEW--97 ANQING--94 DVD--93 SYSMAN--95
2.3 使用 service_name:dave 將2.2 節的dbUrl 改成如下: String dbUrl = "jdbc:oracle:thin:@//127.0.0.1:1521/dave"; 輸出結果: MGMT_VIEW--97 ANQING--94 DVD--93 SYSMAN--95 如果在11g裡遇到如下錯誤: 測試運行Java 類,報錯: java.sql.SQLException: The Network Adapter could not establish the connection 可以嘗試更換對應的 jdbc connection driver,官網的說明如下: JDBC Thin Driver 11g Causes"Java.Sql.Sqlexception: Io Exception: The Network Adapter Could NotEstablish The Connection" While Connecting to Oracle Database 11g [ID947653.1] Change the JDBC connection driver class inyour application server from: oracle.jdbc.driver.OracleDriver to oracle.jdbc.OracleDriver 2.4 使用TNS name: dvd String dbUrl = "jdbc:oracle:thin:@dvd"; 報錯如下: java.sql.SQLException: Unknown host specified 該問題是因為JVM 沒有oracle.net.tns_admin的system property。 解決方法有2種: 方法一:在啟動VM 時添加如下參數: -Doracle.net.tns_admin=D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN 方法二:在java 代碼裡添加: System.setProperty("oracle.net.tns_admin","D:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\NETWORK\\ADMIN"); 添加之後,就可以正常在JDBC中使用tnsnama了。
import java.sql.*; public class jdbc { String dbUrl = "jdbc:oracle:thin:@dvd"; // String dbUrl = "jdbc:oracle:thin:@//127.0.0.1:1521/dave"; // String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:newccs"; String theUser = "dave"; String thePw = "dave"; Connection c = null; Statement conn; ResultSet rs = null; public jdbc() { try { System.setProperty("oracle.net.tns_admin","D:\\app\\Administrator\\product\\11.2.0\\dbhome_1\\NETWORK\\ADMIN"); Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); // Class.forName("oracle.jdbc.OracleDriver").newInstance(); c = DriverManager.getConnection(dbUrl, theUser, thePw); conn = c.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public boolean executeUpdate(String sql) { try { conn.executeUpdate(sql); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } public ResultSet executeQuery(String sql) { rs = null; try { rs = conn.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public void close() { try { conn.close(); c.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ResultSet rs; jdbc conn = new jdbc(); rs = conn.executeQuery("select * from dave where rownum<5"); try { while (rs.next()) { System.out.println(rs.getString("username")+"--"+rs.getString("user_id")); } } catch (Exception e) { e.printStackTrace(); } } }