【问题标题】:Cannot load Oracle or MySQL driver in servlet through Eclipse无法通过 Eclipse 在 servlet 中加载 Oracle 或 MySQL 驱动程序
【发布时间】:2015-11-07 01:08:48
【问题描述】:

我已经为这个问题苦苦挣扎了几个小时并且感到沮丧。在这里和其他网站上看到了一些带有建议的帖子,但它们似乎都不适合我。

我正在编写一些 servlet。我在 JBoss 7.1 服务器上使用 Eclipse。那部分运行良好。 Servlet 很好用。

我正在尝试连接到 Oracle XE 数据库。 Oracle 已全部配置完毕,我的表也已设置完毕。

我正在尝试让我的 servlet 连接到 Oracle 数据库,但在该过程的第一步它失败了;它不会加载驱动程序。名为 LoginAction 的 servlet 尝试通过调用另一个类中的静态方法来建立数据库连接,该类在同一个包的不同文件中定义。下面是负责创建数据库连接的方法:

protected static Connection getDatabaseConnection()
{
    boolean driverloaded = true;

    //Load the driver.
    try
    {
        Class.forName("oracle.jdbc.OracleDriver");
    }
    catch(ClassNotFoundException e)
    {
        System.out.println("Failed to load driver!");
        driverloaded = false;
    }

    if(driverloaded)
        System.out.println("loaded driver!");
    else
        return null;

    //Create and return a database Connection.
    try
    {
        return DriverManager.getConnection(dbURL,dbUser,dbPass);
    }
    catch(SQLException e)
    {
        System.out.println("Problem making database connection!");
        return null;
    }
}

如果我将上述类作为独立的 Java 程序运行,则驱动程序会加载。我稍后在文件中有一个我计划稍后删除的主函数,它只调用 getDatabaseConnection() 函数。

我做了以下事情:

确保 Oracle 驱动程序位于类路径中。通过Eclipse,右键单击Workspace中的项目,进入Properties,选择Java Build Path,点击Libraries选项卡,点击Add外部 JARs,将文件 OJDBC14.jar 添加到类路径。

将 OJDBC14.jar 文件放在 WEB-INF/lib 文件夹中。

不好。

我也尝试使用 MySQL。改为 Class.forName("com.mysql.jdbc.Driver") 并加载了“mysql-connector-java-5.1.25-bin.jar”文件。

这是我的 .classpath 文件,所以我很确定驱动程序在类路径中。

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.jboss.ide.eclipse.as.core.server.runtime.runtimeTarget/JBoss 7.1 Runtime">
		<attributes>
			<attribute name="owner.project.facets" value="jst.web"/>
		</attributes>
	</classpathentry>
	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
	<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
	<classpathentry exported="true" kind="lib" path="C:/Dropbox/Projects/Eintern/Eintern_servlets/intlops/ClientIntakeSystem/WebContent/WEB-INF/lib/ojdbc14.jar"/>
	<classpathentry exported="true" kind="lib" path="C:/Dropbox/Projects/Eintern/Eintern_servlets/intlops/ClientIntakeSystem/WebContent/WEB-INF/lib/mysql-connector-java-5.1.25-bin.jar"/>
	<classpathentry kind="output" path="build/classes"/>
</classpath>

我只是没有想法。我真的不明白,因为我以前写过连接到数据库的 servlet。当我这样做时,我使用的是 Tomcat 而不是 JBoss……这是我能想到的唯一区别。

【问题讨论】:

    标签: mysql eclipse oracle servlets


    【解决方案1】:

    尝试检查部署程序集并确保添加要导出到构建路径的外部库。

    【讨论】:

    • 不确定你的意思是什么。如上所述,我已经通过 Eclipse 通过 Properties > Java Build Path > Libraries 选项卡将驱动程序 JAR 文件添加到构建路径中。
    • 我现在才看到您的回复...所以这部分是指您在 Eclipse 上本地编译时编译和运行的内容,但是当您部署到服务器时,jar 不是专门部署的除非您在部署程序集下指定。右键单击 Project Properties > Deployment Assembly > Add... > Java Build Path Entries。
    猜你喜欢
    • 2013-05-08
    • 1970-01-01
    • 2016-05-11
    • 2017-01-15
    • 2015-07-24
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多