gpt4 book ai didi

mysql - 在我的 JUnit 测试中无法破解 "java.sql.SQLException: No suitable driver"

转载 作者:行者123 更新时间:2023-11-29 14:19:42 24 4
gpt4 key购买 nike

我正在使用 Hibernate 4.1.5.SP1、Hibernate Validator 4.3.0.Final 和 JUnit 4.8.1。当我运行 JUnit 测试时,在尝试设置事务时收到异常“java.sql.SQLException:找不到 jdbc:mysql://localhost:3306/myproject 的合适驱动程序”。

@Before
public void setUpDAOTest() {
final Configuration configuration = new Configuration();
configuration.configure("junit_hibernate.cfg.xml")
.setProperty("javax.persistence.validation.mode", "none")
.setProperty("hibernate.show_sql", "false");
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
ThreadLocalSessionContext.bind(session);
tx = session.beginTransaction(); // exception thrown here

异常表明我的 URL 格式错误,但对我来说似乎是正确的。另外,我已经验证数据库正在运行,并且可以在命令行上连接到它。我的 Hibernate 配置文件是

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

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myproject</property>
<property name="hibernate.connection.username">myproject</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="javax.persistence.validation.mode">none</property>

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

<mapping class="com.follett.fdr.myproject.model.UserRole" />
<mapping class="com.follett.fdr.myproject.model.Organization" />
<mapping class="com.follett.fdr.myproject.model.Product" />
<mapping class="com.follett.fdr.myproject.model.State" />
<mapping class="com.follett.fdr.myproject.model.Country" />
<mapping class="com.follett.fdr.myproject.model.AccessCode" />
<mapping class="com.follett.fdr.myproject.model.Contract" />
<mapping class="com.follett.fdr.myproject.model.TrainingLink" />

</session-factory>
</hibernate-configuration>

所以我很困惑。我还能做错什么吗?这是完整的堆栈跟踪......

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myproject
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:193)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:281)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1396)
at org.mainco.subco.orgsclient.dao.AbstractDaoTest.setUpDAOTest(AbstractDaoTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

最佳答案

你的类路径中应该有 mysql 驱动程序 jar,所以 download it并将其包含在您的类路径中

如果您使用 maven 构建,请从 here 获取定义

关于mysql - 在我的 JUnit 测试中无法破解 "java.sql.SQLException: No suitable driver",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11954644/

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