简体   繁体   English

连接 Oracle 10g Express Edition 和 Java

[英]connecting Oracle 10g Express Edition and Java

I want to connect Oracle 10g Express Edition and Java, the steps that I have followed are:我想连接Oracle 10g Express Edition和Java,我遵循的步骤是:

Configure my classpath with the following files:使用以下文件配置我的类路径:

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

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

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

Then I have tried the following program to connect it with OCI driver:然后我尝试了以下程序将其与 OCI 驱动程序连接:

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:并且还使用 Thin 驱动程序:

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"); Class.forName("oracle.jdbc.driver.OracleDriver");

Any help?有什么帮助吗? Thanks谢谢

First, remove C:\\Program Files\\Java\\jdk1.7.0_01\\bin from your classpath.首先,从类路径中删除C:\\Program Files\\Java\\jdk1.7.0_01\\bin 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?您是否使用 IDE(例如 Eclipse)来运行它? 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).在 Eclipse 中,您可以通过查看为运行您的应用程序而创建的启动配置来获取此信息(请参阅“类路径”选项卡)。

If you set the classpath through the command-line, then it's possible that the blank inside Program Files is the problem.如果您通过命令行设置类路径,则Program Files中的空白可能是问题所在。 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我正在使用 ojdb6.jar 而不是 ojdbc14.jar,它对我来说很好用,这是我的代码

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());
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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