-
1. Re: Class Not Found Exception for com.mysql.jdbc.Driver
jaikiran Jun 9, 2016 11:23 PM (in response to jseanjensen)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
methodsgetConnection
andgetDrivers
have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the fileMETA-INF/services/java.sql.Driver
. This file contains the name of the JDBC drivers implementation ofjava.sql.Driver
. For example, to load themy.sql.Driver
class, theMETA-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 usingClass.forName()
will continue to work without modification. -
2. Re: Class Not Found Exception for com.mysql.jdbc.Driver
jseanjensen Jun 10, 2016 2:54 PM (in response to jaikiran)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 Jun 13, 2016 3:44 AM (in response to jseanjensen)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);
}
....