简体   繁体   English

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

[英]Java EE ClassNotFoundException for MysqlDataSource. What am I doing wrong?

I want to connect to my MySQL database, and query some data. 我想连接到我的MySQL数据库,并查询一些数据。 I am getting the following error: 我收到以下错误:

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]"}}

I'm using maven in my project. 我在项目中使用Maven。 This is my pom.xml file: 这是我的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>

This is my web.xml file: 这是我的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>

And this is my bean class, in which I'd like to access my database: 这是我的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;
    }
}

Thanks in advance! 提前致谢!

The ClassNotFoundException means that the Java runtime (the JVM) that runs your application server (Java EE server) is somehow not able to find the class com.mysql.jdbc.jdbc2.optional.MysqlDataSource . ClassNotFoundException意味着运行您的应用程序服务器(Java EE服务器)的Java运行时(JVM)无法以某种方式找到类com.mysql.jdbc.jdbc2.optional.MysqlDataSource This class is a part of the MySQL JDBC Driver . 此类是MySQL JDBC Driver的一部分。 Since this class is not available when your bean's method getQuestions() is called, the exception is thrown. 由于在调用bean的方法getQuestions()时此类不可用,因此将引发异常。

There are multiple ways in which you can fix this: 您可以通过多种方式解决此问题:

  1. Make your application self contained . 使您的应用程序自成体系 This means that you bundle all the classes that your application needs along with it. 这意味着您将应用程序需要的所有类都捆绑在一起。 This is the whole point of a Java .ear (enterprise application archive). 这是Java .ear(企业应用程序档案)的全部内容。 Doing this (in theory) means that your application can be ported rather easily. 这样做(理论上)意味着您的应用程序可以很容易地移植。 To do this, you can assemble your app in such a way that you specify a runtime dependency on MySQL connector (see mvnrepository for the snippet and add <scope>runtime</scope> ) in a <dependency> element in your pom.xml. 为此,您可以通过以下方式组装应用程序:在pom.xml的<dependency>元素中指定MySQL连接器上的运行时依赖项(请参见mvnrepository以获取代码段并添加<scope>runtime</scope> )。 。 Then you can use the Maven ear plugin if you are assembling a .ear or Maven war plugin if you are assembling a .war to bundle everything up. 然后, 如果要组装.ear,则可以使用Maven耳朵插件;如果要 组装.war ,则可以使用Maven war插件来捆绑所有东西。
  2. Make the MySQL driver jar a part of your application server's classpath. 使MySQL驱动程序jar成为应用服务器的类路径的一部分。 That way, other applications can leverage those classes (you may have more than one app using MySQL, for instance). 这样,其他应用程序就可以利用这些类(例如,您可能有多个使用MySQL的应用程序)。 Application servers have this facility so that these classes are made available to apps. 应用程序服务器具有此功能,因此这些类可用于应用程序。 Read up your app server's documentation to see how to do it. 阅读您的应用服务器的文档以了解操作方法。

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

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