gpt4 book ai didi

java - Hibernate - 找不到适合 jdbc :mysql//* 的驱动程序

转载 作者:可可西里 更新时间:2023-11-01 06:39:12 24 4
gpt4 key购买 nike

我正在尝试在 Web 项目引用的 Java 项目中运行 hibernate 。很简单,只是我无法 hibernate 以连接到我的数据库。我有很多人描述了这个问题并得到了大量的答案,但似乎没有一个对我有用。

这就是我的 hibernate.cfg.xml 的样子:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql//localhost:3306/dbname</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">commander</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">50</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Enable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

<mapping resource="com/amakena/profile/mapping/Credentials.hbm.xml"/>
<mapping resource="com/amakena/profile/mapping/Profile.hbm.xml"/>
<mapping resource="com/amakena/profile/mapping/City.hbm.xml"/>
<mapping resource="com/amakena/profile/mapping/Country.hbm.xml"/>
<mapping resource="com/amakena/profile/mapping/Gender.hbm.xml"/>
</session-factory>
</hibernate-configuration>

我将代码调试到 DriverManagerConnectionProvider 类,在尝试 getConnection() 方法时抛出异常

Connection conn = DriverManager.getConnection(url, connectionProps);

我检查了 url 和 connectionProps,它们是 hibernate.cfg.xml 中的那些

我在我的 tomcat\lib 文件夹中添加了 mysql-connector-java-3.1.14-bin,我可以在 eclipse 的 Libraries>Apache Tomcat v6.0> 下看到它。我还将它添加到我的 %Javahome%\lib\ext 和我的 CLASSPATH 变量(我知道这是无用的,因为我有点绝望)。

我通过在 session.beginTransaction() 之前运行以下代码确保我的驱动程序已正确安装,这反过来会导致 DriverManagerConnectionProvider.getConnection()

Class.forName("com.mysql.jdbc.Driver");
Properties connectionProperties = new Properties();
connectionProperties.put("user", "root");
connectionProperties.put("password", "root");
DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", connectionProperties);

我注意到发出第一行会导致相同的异常。

抛出异常

WARNING: Method execution failed: 
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
at $Proxy0.beginTransaction(Unknown Source)
at com.amakena.profile.dao.CredentialsDao.hydrate(CredentialsDao.java:45)
at com.amakena.profile.ProfileManager.login(ProfileManager.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.directwebremoting.impl.ExecuteAjaxFilter.doFilter(ExecuteAjaxFilter.java:34)
at org.directwebremoting.impl.DefaultRemoter$1.doFilter(DefaultRemoter.java:428)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:431)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:283)
at org.directwebremoting.servlet.PlainCallHandler.handle(PlainCallHandler.java:52)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)
at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/dbname
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 37 more
WARNING: --Erroring: batchId[24] message[org.hibernate.exception.JDBCConnectionException: Cannot open connection]

我很感激我能得到的所有帮助。提前致谢。

使用的版本:Tomcat v6.0 - Hibernate 3.6.6 - Java 6 - Eclipse 3.5.2 R35x

最佳答案

你忘记了“mysql”后面的冒号:

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

这是 doc对于 URL 格式:

jdbc:mysql://[host][,failoverhost...][:port]/[database]

没有合适的驱动程序总是意味着驱动程序 JAR 已加载,但您的 URL 语法不正确。

关于java - Hibernate - 找不到适合 jdbc :mysql//* 的驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6969970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com