Java/Tomcat与Oracle 10g连接

发布于 2024-12-12 03:42:36 字数 1584 浏览 7 评论 0原文

我是 java 和 Oracle 连接的新手,我无法建立从 java/tomcat 服务到 Oracle 数据库的连接。

我正在使用 Java JDK 1.7 和 Oracle 版本:

Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product 
PL/SQL Release 10.2.0.1.0 - Production 
CORE 10.2.0.1.0 Production 
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production 
NLSRTL Version 10.2.0.1.0 - Production 

我向环境变量添加了 CLASSPATH (Windows Server 2003) C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\*

我还将JAVA bin添加到PATH和JAVA_HOME(由TOMCAT使用)

在此文件夹中只有两个文件: ojdbc14.jarojdbc14_g.jar

我的 java 代码:

       import java.sql.*;
       ....
       ....

                        try
                        {                              
                                System.out.println("0");
                                Class.forName("oracle.jdbc.driver.OracleDriver");
                                System.out.println("1");
                                Connection conn = DriverManager.getConnection(
                                  "jdbc:oracle:thin:@//127.0.0.1:1521/xe",
                                  "username", "password");
                                System.out.println("2");                       
                        }
                        catch (Exception e)
                        {
                                //e.printStackTrace();
                            System.out.println("exc");
                        }

输出:(似乎他在Class.forName

0
exc

I'm new to java and Oracle connection I can't establish a connection from java/tomcat service to Oracle database.

I'm using Java JDK 1.7 and Oracle Version:

Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product 
PL/SQL Release 10.2.0.1.0 - Production 
CORE 10.2.0.1.0 Production 
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production 
NLSRTL Version 10.2.0.1.0 - Production 

I added a CLASSPATH to the Environment Variables (Windows Server 2003)
C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\*

I also added the JAVA bin to PATH and JAVA_HOME (used by TOMCAT)

In this folder there are only two files: ojdbc14.jar and ojdbc14_g.jar

My java Code:

       import java.sql.*;
       ....
       ....

                        try
                        {                              
                                System.out.println("0");
                                Class.forName("oracle.jdbc.driver.OracleDriver");
                                System.out.println("1");
                                Connection conn = DriverManager.getConnection(
                                  "jdbc:oracle:thin:@//127.0.0.1:1521/xe",
                                  "username", "password");
                                System.out.println("2");                       
                        }
                        catch (Exception e)
                        {
                                //e.printStackTrace();
                            System.out.println("exc");
                        }

Output: (It seems that he throw an exception on Class.forName

0
exc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏有森光若流苏 2024-12-19 03:42:36

Tomcat 和所有 Java EE 应用服务器完全忽略您的 CLASSPATH 环境变量。对于 IDE 来说也是如此。我使用的任何机器上都没有一个。

您也不应该更改任何脚本来执行此操作。

每个 Java EE 应用服务器都有一个类加载器层次结构。

第一个运行的是引导类加载器。

接下来是服务器类加载器。这些 JAR 的正确位置是 Tomcat 服务器 /lib 目录。 /lib 目录包含所有部署通用的所有 JAR。它们在启动时在 WAR 文件之前加载。

每个部署的 WAR 文件都有一个类加载器。服务器类加载器运行后,Tomcat 会将您放入 WEB-INF/lib 中的所有 JAR 以及放入 WEB-INF/classes 下的所有 .class 文件视为您的项目 CLASSPATH。

一旦您开始工作,接下来就是学习如何使用 JNDI 查找。您发布的代码不是一个好主意。让 Tomcat 为您管理连接池。

Tomcat, and all Java EE app servers, completely ignore your CLASSPATH environment variable. Same for IDEs. I don't have one on any machine that I use.

You should not be altering any scripts to do this, either.

Every Java EE app server has a hierarchy of class loaders.

The first to run is the bootstrap class loader.

Next is the server class loader. The right place for those JARs is the Tomcat server /lib directory. The /lib directory contains all the JARs that are common to all deployments. They're loaded before the WAR files on startup.

There's a class loader for each WAR file deployed. After the server class loader runs, Tomcat treats all the JARs that you put in the WEB-INF/lib and all the .class files you put under WEB-INF/classes as your project CLASSPATH.

Once you've gotten that to work, the next thing is to learn about how to use JNDI lookups. The code you posted isn't a good idea. Let Tomcat manage a connection pool for you.

我一直都在从未离去 2024-12-19 03:42:36

您需要按如下方式设置类路径:

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

类路径需要知道要使用的 jar,这是生产驱动程序,带有 _g 的驱动程序用于调试目的。使用通配符对于设置 java 类路径不起作用。为了避免配置类路径,您只需将 ojdbc14.jar 复制到 $tomcat.home/lib 目录即可。

You need to set the classpath as follows:

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

the classpath needs to know the jar to use, this is the production driver and the one with the _g is for debugging purpose. Using a wildcard does not work for setting the java classpath. To avoid the need of configuring the classpath you can just copy the ojdbc14.jar to the $tomcat.home/lib directory.

或十年 2024-12-19 03:42:36

只需打印 e.printStackTrace(); 即可看到天气异常发生...
如果它在 Class.forName() 中,则意味着类路径设置不正确...
一切都很好..只需检查..在类路径中

Just print Out e.printStackTrace(); and see weather exception occur...
if it inClass.forName() then that means class path is not set properly...
every thing seam fine.. just check..in class path

屋檐 2024-12-19 03:42:36

司机名字是worng。

它必须是 oracle.jdbc.OracleDriver,而不是您正在使用的 oracle.jdbc.driver.OracleDriver。

Driver name is worng.

It must be oracle.jdbc.OracleDriver instead of what you are using oracle.jdbc.driver.OracleDriver.

童话里做英雄 2024-12-19 03:42:36

您需要将 ojdbc14.jar 文件包含在项目的部署程序集中。使用日食:

项目 -->属性-->部署组装 -->选择/webContent -->添加--> JavaBuildPathEnteries -->下一步--> ojdbc**.jar -->结束。

You need to include the ojdbc14.jar file in your Deployment Assembly in your project. Using eclipse :

proj --> properties --> Deployment Assembly --> select /webContent --> Add --> JavaBuildPathEnteries --> Next --> ojdbc**.jar --> finish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文