ITPub博客

首页 > 应用开发 > Java > How to use JDBC to connect to Oracle DB

How to use JDBC to connect to Oracle DB

原创 Java 作者:_JustDoIt_ 时间:2016-08-09 11:41:12 0 删除 编辑

Example of Using OCI:

  1. // This example comes from Oracle official Docs.
  2. import java.sql.*;
  3. import oracle.jdbc.*;
  4. import oracle.jdbc.pool.OracleDataSource;

  5. // We import java.io to be able to read from the command line import java.io.*;
  6. class JdbcCheckup {
  7.     public static void main(String args[]) throws SQLException, IOException {
  8. // Prompt the user for connect information
  9.         System.out.println("Please enter information to test connection to the database");
  10.         String user;
  11.         String password;
  12.         String database;

  13.         user = readEntry("user: ");
  14.         int slash_index = user.indexOf('/');
  15.         if (slash_index != -1)
  16.         {
  17.             password = user.substring(slash_index + 1);
  18.             user = user.substring(0, slash_index);
  19.         }
  20.         else
  21.             password = readEntry("password: ");

  22.         database = readEntry("database(a TNSNAME entry): ");
  23.         System.out.print("Connecting to the database...");
  24.         System.out.flush();
  25.         System.out.println("Connecting...");
  26. // Open an OracleDataSource and get a connection
  27.         OracleDataSource ods = new OracleDataSource();
  28.         ods.setURL("jdbc:oracle:oci:@" + database);
  29.         ods.setUser(user);
  30.         ods.setPassword(password);
  31.         Connection conn = ods.getConnection();
  32.         System.out.println("connected.");
  33. // Create a statement
  34.         Statement stmt = conn.createStatement();
  35. // Do the SQL "Hello World" thing
  36.         ResultSet rset = stmt.executeQuery("select 'Hello World' from dual");
  37.         while (rset.next())
  38.             System.out.println(rset.getString(1));
  39. // close the result set, the statement and the connection
  40.         rset.close();
  41.         stmt.close();
  42.         conn.close();
  43.         System.out.println("Your JDBC installation is correct.");
  44.     }
  45. // Utility function to read a line from standard input
  46.     static String readEntry(String prompt){
  47.         try {
  48.             StringBuffer buffer = new StringBuffer();
  49.             System.out.print(prompt);
  50.             System.out.flush();
  51.             int c = System.in.read();
  52.             while(c!='\n'&&c!=-1) {
  53.                 buffer.append((char)c); c = System.in.read();
  54.             }
  55.             return buffer.toString().trim();
  56.         }
  57.         catch(IOException e) {
  58.             return "";
  59.         }
  60.     }
  61. }

Example of Using Thin:

  1. jdbc:oracle:thin:[scott/tiger]@HostnameOrIP:PortNum:SID            
  2.                                        -- using [scott/tiger] means u can use it or not
  3. jdbc:oracle:thin:[scott/tiger]@HostnameOrIP:PortNum/service_name   -- using service_name
  4. jdbc:oracle:thin:[scott/tiger]@(DESCRIPTION =                      -- using tns string
       (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.11)(PORT = 1521))
       (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.12)(PORT = 1521))
       (LOAD_BALANCE = no)
       (FAILOVER = ON)
       (CONNECT_DATA =
         (SERVER = DEDICATED)
         (SERVICE_NAME = oradb)
         (FAILOVER_MODE =
           (TYPE = SESSION)
           (METHOD = BASIC)
         )
       )
     )

  5. jdbc:oracle:thin:[scott/tiger]@ldap://HostnameOrIP:PortNum/service_name,cn=OracleContext,dc=com    
  6.         --using ldap
  7. jdbc:oracle:thin:[scott/tiger]@ldaps://HostnameOrIP:PortNum/service_name,cn=OracleContext,dc=com   
  8.         --using ldap+ssl

Example of Using tnsnames.ora:


  1. -- jdbc using oci --
  2. String url = "jdbc:oracle:oci:[scott/tiger]@mydb");
  3. ods.setURL(url);
  4. Connection conn = ods.getConnection();
  5. -- jdbc using thin --
  6. jdbc:oracle:thin:[scott/tiger]@mydb;

  7. Jackson$ java -D oracle.net.tns_admin=$ORACLE_HOME/network/admin
  8. Jackson$ cat $ORACLE_HOME/network/admin/tnsnames.ora
  9. mydb = 
  10.  (DESCRIPTION =
        (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.11)(PORT = 1521))
        (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.12)(PORT = 1521))
        (LOAD_BALANCE = no)
        (FAILOVER = ON)
        (CONNECT_DATA =
          (SERVER = DEDICATED)
          (SERVICE_NAME = oradb)
          (FAILOVER_MODE =
            (TYPE = SESSION)
            (METHOD = BASIC)
          )
        )
     ) 


Maybe You Will Need to Read This When You're Using Oracle 12c.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25521690/viewspace-2123201/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-03-15

  • 博文量
    28
  • 访问量
    26064