引起:java.sql.SQLException: 没有找到适合 jdbc:mysql://localhost:3306/domain 的驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20806698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/domain
提问by Code Junkie
Hello I'm using the following
你好,我正在使用以下
hibernate-core-4.1.2.Final.jar
hibernate-core-4.1.2.Final.jar
mysql-connector-5.1.6.jar
mysql-connector-5.1.6.jar
Both can be found in my project lib directory.
两者都可以在我的项目 lib 目录中找到。
I have the following hibernate.cg.xmlconfiguration.
我有以下hibernate.cg.xml配置。
<?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?
我不想使用 JNDI 来实现管理层希望保持应用程序尽可能便携的事实,那么我错过了什么才能让它与 jdbc 一起工作?我是否需要对 tomcat 进行任何类型的配置?
Tomcat Lib
Tomcat 库
回答by elbuild
Try putting mysql-connector-5.1.6.jardirectly into the lib folder of tomcatand restarting it.
尝试将mysql-connector-5.1.6.jar直接放到tomcat的lib文件夹下,重启即可。
回答by Rogue
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)
我的课程可能有误,但如果com.mysql.jdbc.Driver
不起作用,您也可以尝试com.mysql.JDBC
或com.mysql.jdbc
(基于 SQLite 的调用方式)
回答by cmd
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/libdirectory.
发生异常是因为 mysql 数据库驱动程序不在您的类路径中。将其添加到您的类路径以修复问题。由于您使用的是 tomcat,您可以简单地将其添加到tomcat/lib目录中。
回答by bradleyfitz
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.
在可移植性方面,我用我的war打包了数据库驱动,所以它是自包含的。这使得跨多个环境的部署变得更加容易,如果另一个开发人员想要在本地构建和运行,他们只需要将战争放入 Tomcat 中即可。将数据库驱动程序放在 WEB-INF/lib 文件夹中。
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.
此外,在可移植性方面,我推荐 JNDI...这样,当您将其部署到另一台服务器时,您不必编辑您的休眠配置文件,并且它可以在您的战争中保持打包状态。您只需在 Tomcat 配置中添加 JNDI 引用。
回答by DoesEatOats
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
我建议将您的驱动程序放在您放置 JDK 扩展目录的位置。请参阅: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%
一旦完成,我会鼓励你从你的提示类型:echo %CLASSPATH%