简体   繁体   English

JBOSS AS 7和com.mysql.jdbc.Driver错误

[英]JBOSS AS 7 and com.mysql.jdbc.Driver error

I am using JBOSS AS 7.0.2. 我正在使用JBOSS AS 7.0.2。 I want to create a connection to mysql server the old way (I know I should use JNDI, but I just need it for some quick thing): 我想用旧的方式创建到mysql服务器的连接(我知道我应该使用JNDI,但是我只需要它可以快速使用):

Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(.....);

I have mysql-connector-java-5.1.18-bin.jar included under C:\\Java\\jboss-as-7.0.2.Final\\standalone\\deployments. 我在C:\\ Java \\ jboss-as-7.0.2.Final \\ standalone \\ deployments下包含了mysql-connector-java-5.1.18-bin.jar。 I also added it under build path. 我还在构建路径下添加了它。 I can see my tables through Data Source Explorer. 我可以通过数据源资源管理器查看我的表。 But when I try to connect to it through code, it throws exception when it tries to create new instance: 但是,当我尝试通过代码连接到它时,在尝试创建新实例时会引发异常:

Class.forName("com.mysql.jdbc.Driver").newInstance();

The ClassNotFoundException catch clause catches this exception: ClassNotFoundException catch子句捕获以下异常:

Error: com.mysql.jdbc.Driver from [Module "deployment.Seminarska.war:main" from Service Module Loader]

I have found this thread with this solution: 我发现这个线程这个解决方案:

As a rule you should not be including your JDBC drivers in your war file. 通常,您不应在war文件中包括JDBC驱动程序。

I suggest you mark the driver as provided and add it to the lib directory of the server. 我建议您将驱动程序标记为已提供,并将其添加到服务器的lib目录中。

What does mean mark the driver as provided ? 将驱动程序标记为已提供是什么意思? How does one do that? 怎么做到的?

"Mark the driver as provided" relates to how you specify the dependencies of your module if you use Maven as a build tool. 如果将Maven用作构建工具,则“将驱动程序标记为提供”与您如何指定模块的依赖项有关。 Dependencies can have different scopes and "provided" means that that particular dependency is needed for compiling but will be provided by the runtime environment (typically an application server) so it should not be included in the packaged artifact (like a war). 依赖关系可以具有不同的范围,“提供”意味着编译需要特定的依赖关系,但是依赖关系将由运行时环境(通常是应用程序服务器)提供,因此不应将其包含在打包的工件中(如战争)。

More on Maven dependency scopes here: 有关Maven依赖范围的更多信息,请参见:

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Regarding how to configure datasources in JBoss AS 7 you can find instructions here: 关于如何在JBoss AS 7中配置数据源,您可以在这里找到说明:

https://community.jboss.org/wiki/DataSourceConfigurationInAS7 https://community.jboss.org/wiki/DataSourceConfigurationInAS7

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

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