我想连接到我的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;
}
}
提前致谢!
答案 0 :(得分:1)
ClassNotFoundException
表示运行应用程序服务器(Java EE服务器)的Java运行时(JVM)无法以某种方式找不到类com.mysql.jdbc.jdbc2.optional.MysqlDataSource
。这个类是MySQL JDBC Driver的一部分。由于在调用bean的方法getQuestions()
时此类不可用,因此抛出异常。
有多种方法可以解决此问题:
<scope>runtime</scope>
)
pom.xml中的<dependency>
元素。然后,您可以使用Maven
ear
plugin if you are assembling a .ear或Maven war plugin if you are assembling a .war
捆绑所有东西。