简体   繁体   中英

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

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?

在此输入图像描述

Tomcat Lib

在此输入图像描述

尝试将mysql-connector-5.1.6.jar直接放入tomcatlib文件夹中并重新启动它。

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)

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.

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.

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%

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