简体   繁体   中英

Where to put META-INF/services/java.sql.Driver in web app

The javadoc says "to load the my.sql.Driver class, the META-INF/services/java.sql.Driver file would contain the entry: my.sql.Driver".

Don't webapps typically have a META-INF folder as a sibling of WEB-INF? However, JPA specs are looking for persistence.xml in WEB-INF/classes/META-INF/.

Which place is correct for services/java.sql.Driver?

By the way, I am getting "No suitable Driver" exception with it in both locations.

You should not provide it yourself. The JDBC driver JAR file should already contain it. At least, if it's a JDBC 4.0 compliant driver. This is also explicitly mentioned in javadoc of DriverManager :

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 explictly load JDBC drivers using Class.forName() . Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

If yours doesn't have, then it's apparently not a JDBC 4.0 compliant driver. You'd need to explicitly load the driver yourself, or to upgrade to a JDBC 4.0 compliant driver, or just use a container managed DataSource (which offers more advantages as well, such as connection pooling).

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