Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/domain

a 夏天 提交于 2021-02-08 03:44:57

问题


Hello I'm using the following

hibernate-core-4.1.2.Final.jar

mysql-connector-5.1.6.jar

Both can be found in my project lib directory.

I have the following hibernate.cg.xml configuration.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Defines the SQL dialect used in Hiberante's application -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--Local Database Connection-->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/domain</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">test</property> 

        <property name="hbm2ddl.auto">validate</property>  

        <property name="show_sql">false</property>
        <property name="format_sql">false</property>
        <property name="use_sql_comments">false</property>

        <property name="hibernate.search.default.directory_provider">ram</property> 
    </session-factory>
</hibernate-configuration>

and I'm getting the following exception.

 Caused by: java.sql.SQLException: No suitable driver found for
 jdbc:mysql://localhost:3306/domain     at
 java.sql.DriverManager.getConnection(DriverManager.java:604)   at
 java.sql.DriverManager.getConnection(DriverManager.java:190)   at
 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
    at
 org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
    at
 org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
    ... 145 more

I do not want to use JNDI do to the fact management wants to keep the app as portable as possible, so what am I missing to get this to work with jdbc? Am I required to do any kind of configurations to tomcat?

enter image description here

Tomcat Lib

enter image description here


回答1:


Try putting mysql-connector-5.1.6.jar directly into the lib folder of tomcat and restarting it.




回答2:


Have you tried calling the driver class?:

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

How to call it:

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
    // error out
}
Connection con = DriverManager.getConnection(/*your connection query*/);

I may have the class wrong, but if com.mysql.jdbc.Driver doesn't work, you can also try com.mysql.JDBC or com.mysql.jdbc (basing off how SQLite calls it)




回答3:


Did you edit the config to obscure the connection string? Your hibernate config has a different database name than the error:

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/domain</property>

Caused by: java.sql.SQLException: No suitable driver found for
 jdbc:mysql://localhost:3306/etss

In terms of portability, I package the database driver with my war, so it is self contained. This makes for deployment across multiple environments much easier and if another developer wants to build and run locally, they just have to drop the war into Tomcat and go. Place the database driver in your WEB-INF/lib folder.

Also, in terms portability, I recommend JNDI... that way you do not have to edit your hibernate config file when you deploy it to another server and it can stay packaged in your war. You just add the JNDI reference in the Tomcat config.




回答4:


The exception occurs because the mysql database driver is not on your classpath. Add it to your classpath to repair the issue. Since you are using tomcat you can simply add it to the tomcat/lib directory.




回答5:


I would suggest putting your drivers in/at where you have place JDK's Extension directory. Please see:http://www3.ntu.edu.sg/home/ehchua/programming/howto/ErrorMessages.html#zz-4.1

Once that is done I would encourage you to from your prompt type: echo %CLASSPATH%



来源:https://stackoverflow.com/questions/20806698/caused-by-java-sql-sqlexception-no-suitable-driver-found-for-jdbcmysql-loca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!