3 Replies Latest reply on Jun 13, 2016 3:44 AM by jaikiran pai

    Class Not Found Exception for com.mysql.jdbc.Driver

    Sean Jensen Newbie

      I'm using Wildfly 10 and my datasource in the standalone.xml is:

       

                  <datasources>

                      <datasource jta="true" jndi-name="java:/MySqlDS" pool-name="MySQL_AAA" enabled="true" use-ccm="true">

                          <connection-url>jdbc:mysql://localhost:3306/aaa</connection-url>

                          <driver-class>com.mysql.jdbc.Driver</driver-class>

                          <driver>mysql-connector-java-5.1.35-bin.jar_com.mysql.jdbc.Driver_5_1</driver>

                          <security>

                              <user-name>root</user-name>

                              <password>root</password>

                          </security>

                          <validation>

                              <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>

                              <background-validation>true</background-validation>

                              <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>

                          </validation>

                      </datasource>

       

      I'm running this code in my Java bean:

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


      and I get this error message:

      10:29:59,210 ERROR [stderr] (default task-15) java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from [Module "deployment.AAA_5.war:main" from Service Module Loader]

       

       

      10:29:59,210 ERROR [stderr] (default task-15) at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)

       

       

      I read documentation that said I needed to have the driver in my lib folder but that didn't work.  I'm sure the solution is simple but I don't see it.  Can someone point me in the right direction.  Some documentation that I can use as a resource would be great.

        • 1. Re: Class Not Found Exception for com.mysql.jdbc.Driver
          jaikiran pai Master

          Sean Jensen wrote:

           

          I'm using Wildfly 10 and my datasource in the standalone.xml is:

           

                      <datasources>

                          <datasource jta="true" jndi-name="java:/MySqlDS" pool-name="MySQL_AAA" enabled="true" use-ccm="true">

                              <connection-url>jdbc:mysql://localhost:3306/aaa</connection-url>

                              <driver-class>com.mysql.jdbc.Driver</driver-class>

                              <driver>mysql-connector-java-5.1.35-bin.jar_com.mysql.jdbc.Driver_5_1</driver>

           

          That version of MySQL JDBC driver is a Type 4 JDBC driver, which means that you don't have to use the Class.forName(...) statement for loading the driver in your code. So you just remove this from your code:

           

          Sean Jensen wrote:

           

          I'm running this code in my Java bean:

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

           

          and things should work fine. More details about that are here https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html

           

          The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver. For example, to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry:

           my.sql.Driver 

          Applications no longer need to explicitly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification. 

          • 2. Re: Class Not Found Exception for com.mysql.jdbc.Driver
            Sean Jensen Newbie

            You were on the right track.  I took that line out and got another error message.  It turns out I didn't know how to get the datasource and once I got it the rest was pretty straightforward.  This is what I ended up using after taking out the Class.forName("java:MySQLDS") line.

            import javax.sql.DataSource;

            import javax.naming.InitialContext;

             

            InitialContext context = new InitialContext();

            DataSource datasource = (DataSource) context.lookup("java:/MySqlDS");

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource.getConnection());

             

            Thank you for taking the time to reply.

            • 3. Re: Class Not Found Exception for com.mysql.jdbc.Driver
              jaikiran pai Master

              Sean Jensen wrote:


              InitialContext context = new InitialContext();

              DataSource datasource = (DataSource) context.lookup("java:/MySqlDS");

              JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource.getConnection());

               

               

              Be sure you close that connection when done or else it will cause a connection leak. You can you the try-resources feature of Java to do this, something like:

               

               

              InitialContext context = new InitialContext();

              DataSource datasource = (DataSource) context.lookup("java:/MySqlDS");

              try (final Connection connection = datasource.getConnection();) {

                    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);   

              }

              ....