Java/Tomcat与Oracle 10g连接
我是 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.jar
和 ojdbc14_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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.
您需要按如下方式设置类路径:
类路径需要知道要使用的 jar,这是生产驱动程序,带有 _g 的驱动程序用于调试目的。使用通配符对于设置 java 类路径不起作用。为了避免配置类路径,您只需将 ojdbc14.jar 复制到
$tomcat.home/lib
目录即可。You need to set the classpath as follows:
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.只需打印
e.printStackTrace();
即可看到天气异常发生...如果它在
Class.forName()
中,则意味着类路径设置不正确...一切都很好..只需检查..在类路径中
Just print Out
e.printStackTrace();
and see weather exception occur...if it in
Class.forName()
then that means class path is not set properly...every thing seam fine.. just check..in class path
司机名字是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.
您需要将 ojdbc14.jar 文件包含在项目的部署程序集中。使用日食:
You need to include the ojdbc14.jar file in your Deployment Assembly in your project. Using eclipse :