简体   繁体   中英

connecting Oracle 10g Express Edition and Java

I want to connect Oracle 10g Express Edition and Java, the steps that I have followed are:

Configure my classpath with the following files:

C:\\oraclexe\\app\\oracle\\product\\10.2.0\\server\\jdbc\\lib\\ojdbc14.jar

C:\\Program Files\\Java\\jdk1.7.0_01\\bin

C:\\oraclexe\\app\\oracle\\product\\10.2.0\\server\\BIN

Then I have tried the following program to connect it with OCI driver:

import  java.sql.*;

public class OracleOCIConnection
{
   public static void main(String args[])
   {
      try
      {
      // load oracle driver
         Class.forName("oracle.jdbc.driver.OracleDriver");
      // connect using Native-API (OCI) driver
         Connection con = DriverManager.getConnection("jdbc:oracle:oci8:@","hr","hr" );
         System.out.println("Connected Successfully To Oracle using OCI driver");
         con.close();
      }
         catch(Exception ex)
         {
            ex.printStackTrace();
         }
   }
}

and also this using the Thin driver:

import  java.sql.*;

public class OracleThinConnection
{
   public static void main(String args[])
   {
      try
      {
      // load oracle driver
         Class.forName("oracle.jdbc.driver.OracleDriver");
      // connect using Thin driver
         Connection con =  DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
         System.out.println("Connected Successfully To Oracle");
         con.close();
      }
         catch(Exception ex)
         {
            ex.printStackTrace();
         }
   }
}

In both cases the program compiles, but the line that throws an error is:

Class.forName("oracle.jdbc.driver.OracleDriver");

Any help? Thanks

First, remove C:\\Program Files\\Java\\jdk1.7.0_01\\bin from your classpath. It has nothing to do with it.

Second, the problem is with your runtime classpath. Remember, compile-time classpath and runtime classpath are two different things. Are you using an IDE (such as Eclipse) to run this? if so, check to see which classpath entries take effect on runtime. In Eclipse, you can get this information by looking at the Launch Configuration that was created to run your application (see the "Classpath" tab).

If you set the classpath through the command-line, then it's possible that the blank inside Program Files is the problem. Try surrounding the entire classpath argument with quotes.

I'm using ojdb6.jar instead of ojdbc14.jar and it works fine for me, here is my code

try{
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            String dbAddress = "localhost";
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:hr/hr@"+dbAddress+":1521/XE");

            if(!con.isClosed()){
                System.out.println("Connection Successful");
            }else{
                System.out.println("Connection is Closed);
            }
        }
        catch(Exception ex){
            System.out.println("Error :"+ex.getMessage());
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM