gpt4 book ai didi

java - 如何在应用程序启动期间处理 org.hibernate.exception.GenericJDBCException?

转载 作者:行者123 更新时间:2023-11-29 05:55:26 26 4
gpt4 key购买 nike

我正在学习 hibernate/spring mvc,但启动服务器时出现异常。我在 intellij 工作,这个数据库连接到我的 IDE 并且工作完美(“测试连接”也可以),但是当我试图在配置文件中创建一个 bean 时,它根本不工作。可能是什么问题?

@Configuration
@EnableTransactionManagement
@ComponentScan("com.packt.webstore")
public class RootApplicationContextConfig {
@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setUrl("jdbc:mysql://localhost:3306/shop");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}

@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new
LocalSessionFactoryBean();
factoryBean.setDataSource(getDataSource());

Properties props = new Properties();
props.put("hibernate.show_sql", "true");
props.put("hibernate.hbm2ddl.auto", "update");
props.put("hibernate.dialect",
"org.hibernate.dialect.MySQLDialect");
factoryBean.setHibernateProperties(props);
factoryBean.setPackagesToScan("com.packt.webstore");
return factoryBean;
}

@Bean
public HibernateTransactionManager getTransactionalManager() {
HibernateTransactionManager transactionManager = new
HibernateTransactionManager();

transactionManager.setSessionFactory(getSessionFactory()
.getObject());
return transactionManager;
}
}

异常日志:

Error creating bean with name 'productController': Unsatisfied 
dependency expressed through field 'productService': Error creating
bean with name 'productServiceImpl': Unsatisfied dependency expressed
through field 'products': Error creating bean with name
'productDaoImpl': Unsatisfied dependency expressed through field
'factory': Error creating bean with name 'getSessionFactory' defined in
com.packt.webstore.config.RootApplicationContextConfig: Invocation of
init method failed; nested exception is
org.hibernate.exception.GenericJDBCException: Unable to open JDBC
Connection for DDL execution;

最佳答案

Unable to open JDBC Connection for DDL execution

告诉您在执行代码时无法与配置的数据库建立连接。这可能是由多种原因造成的,因此我只列出最有可能的原因:

  1. MySQL 根本没有运行。
    • 你开始了吗?通过检查正在运行的进程列表进行验证(取决于操作系统)。
  2. MySQL 未监听默认 端口 3306。
    • 你改了吗?如果是这样,请相应地更改您的代码/配置。
  3. 鉴于 MySQL 正在运行并监听(端口 3306),用户 root 的密码不是root
    • 您是否将密码设置为 root?使用这些凭据在本地通过 mysql CLI 连接。如果可行,您可以排除密码不匹配的情况。
  4. 用户 root 没有权限连接到名称为 shop 的数据库。
    • 您是否更改了用户 root 的权限以限制对 shop 数据库的访问?如果是,请检查和/或更改此用户对本地连接的权限 (GRANT)。

希望这份 list 对您有所帮助。

更新-1

根据 mysql-connector.jar 的驱动程序版本,您必须仔细检查/选择驱动程序类的名称:

  • MySQL 连接器/J 5.x
    • jdbc.driver_class => com.mysql.jdbc.Driver
  • MySQL 连接器/J 6.x+
    • jdbc.driver_class => com.mysql.cj.jdbc.Driver

关于java - 如何在应用程序启动期间处理 org.hibernate.exception.GenericJDBCException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825858/

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