Three methods for connecting Java JDBC Thin Driver to Oracle

Source: Internet
Author: User

Three methods for connecting Java JDBC Thin Driver to Oracle
1. JDBC connection Oracle explain JDBC application connection Oracle problems, the error is as follows: ORA-12505, TNS: listener does not currently know of SID given in connect descriptor TheConnection descriptor used by the client was. I configured static registration at the DB level, and GLOBAL_DBNAME and SID_NAME are different. The previous configuration is the same, so I did not find this problem. (SID_DESC = (GLOBAL_DBNAME = dave) (ORACLE_HOME = D: \ app \ Administrator \ product \ 11.2.0 \ dbhome_1) (SID_NAME = NEWCCS) Oracle Listener Dynamic Registration and static registration http://www.bkjia.com/database/201302/187441.html After logging on to the Internet, I found that the formats of JDBC Thin Driver has three formats: Format 1: Oracle JDBC Thin using a ServiceName: jdbc: oracle: thin: @ //

Listener.oraSID_LIST_LISTENER = (SID_LIST =   (SID_DESC =     (GLOBAL_DBNAME = dave)         (ORACLE_HOME =D:\app\Administrator\product\11.2.0\dbhome_1)     (SID_NAME = NEWCCS)    )  )Tnsnames.oraDVD = (DESCRIPTION =   (ADDRESS_LIST =     (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))    )   (CONNECT_DATA =     (SERVICE_NAME = dave)    )  )

 

2.2 Test 1, using 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.exe cuteUpdate (SQL); return true;} catch (SQLException e) {e. printStackTrace (); return false ;}} public ResultSet executeQuery (String SQL) {rs = null; try {rs = conn.exe cuteQuery (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.exe cuteQuery ("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 () ;}}--- output normal: MGMT_VIEW--97 ANQING--94 DVD--93 SYSMAN--95

 

2.3 use service_name: dave to change the dbUrl in section 2.2 to the following: String dbUrl = "jdbc: oracle: thin: @ // 127.0.0.1: 1521/dave"; output result: MGMT_VIEW--97 ANQING--94 DVD--93 if the SYSMAN--95 encounters the following error in 11g: Test to run Java class, error: java. SQL. SQLException: The Network Adapter cocould not establish the connection can try to change The corresponding jdbc connection driver. the official website describes JDBC Thin Driver 11g Causes "Java. SQL. sqlexception: Io Exception: The Network Adapter cocould NotEstab Lish 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 uses TNS name: dvd String dbUrl = "jdbc: oracle: thin: @ dvd"; the following error is returned: java. SQL. SQLException: Unknown host specified. This problem occurs because the JVM does not have oracle.net. system property OF tns_admin. There are two solutions: Method 1: Add the following parameter when starting the VM: -Doracle.net. tns_admin = D: \ app \ Administrator \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN Method 2: Add System in java code. setProperty ("oracle.net. tns_admin "," D :\\ app \ Administrator \ product \ 11.2.0 \ dbhome_1 \ NETWORK \ ADMIN "); after adding, you can use tnsnama in JDBC normally.
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();         }      }  }     

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.