MysqlDataSource的Java EE ClassNotFoundException。我究竟做错了什么?

时间:2016-02-28 21:46:35

标签: java mysql maven java-ee datasource

我想连接到我的MySQL数据库,并查询一些数据。我收到以下错误:

2016-02-28 10:42:17,438] Artifact JavaEESecurity:war exploded: Error during artifact deployment. See server log for details. [2016-02-28 10:42:17,438] Artifact JavaEESecurity:war exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"JavaEESecurity_war_exploded.war\".INSTALL"
=> "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"JavaEESecurity_war_exploded.war\".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment \"JavaEESecurity_war_exploded.war\"
    Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: 
    java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource from 
    [Module \"deployment.JavaEESecurity_war_exploded.war:main\" from Service Module Loader]
    Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.jdbc2.optional.MysqlDataSource from [Module \"deployment.JavaEESecurity_war_exploded.war:main\" from Service Module Loader]"}}

我在我的项目中使用maven。这是我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.own.group</groupId>
    <artifactId>myOwnArtifactId</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.13</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <data-source>
        <name>java:global/JavaEESecurity/myDS</name>
        <class-name>com.mysql.jdbc.jdbc2.optional.MysqlDataSource</class-name>
        <server-name>localhost</server-name>
        <database-name>mncpp</database-name>
        <user>root</user>
        <transactional>true</transactional>
        <initial-pool-size>2</initial-pool-size>
        <max-pool-size>10</max-pool-size>
        <min-pool-size>5</min-pool-size>
        <max-statements>0</max-statements>
    </data-source>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

这是我的bean类,我想访问我的数据库:

@Named("questionListBean")
@RequestScoped
public class QuestionListBean {
    List<String> questions;
    @Resource(lookup = "java:global/JavaEESecurity/myDS")
    DataSource ds;

    public List<String> getQuestions() {
        try (Connection connection = ds.getConnection()) {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT question FROM questions");
            questions = new ArrayList<>();

            while(rs.next()) {
                questions.add(rs.getString(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return questions;
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

ClassNotFoundException表示运行应用程序服务器(Java EE服务器)的Java运行时(JVM)无法以某种方式找不到类com.mysql.jdbc.jdbc2.optional.MysqlDataSource。这个类是MySQL JDBC Driver的一部分。由于在调用bean的方法getQuestions()时此类不可用,因此抛出异常。

有多种方法可以解决此问题:

  1. 使您的应用自包含。这意味着你捆绑了 您的应用程序需要的所有类。这是 Java .ear(企业应用程序归档)的重点。 这样做(理论上)意味着可以移植您的应用程序 相当容易。为此,您可以以这种方式组装您的应用程序 您在MySQL连接器上指定运行时依赖项(请参阅 mvnrepository 对于代码段并添加<scope>runtime</scope>) pom.xml中的<dependency>元素。然后,您可以使用Maven ear plugin if you are assembling a .earMaven war plugin if you are assembling a .war 捆绑所有东西。
  2. 使MySQL驱动程序jar成为应用程序服务器类路径的一部分。这样,其他应用程序可以利用这些类(例如,您可能有多个使用MySQL的应用程序)。应用程序服务器具有此功能,以便这些类可供应用程序使用。阅读您的应用服务器的文档,了解如何操作。