简体   繁体   中英

JBOSS AS 7 and com.mysql.jdbc.Driver error

I am using 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):

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. 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:

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.

I suggest you mark the driver as provided and add it to the lib directory of the server.

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. 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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