Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在应用程序启动期间如何处理org.hibernate.exception.genericjdbception?_Java_Mysql_Spring_Hibernate_Jdbc - Fatal编程技术网

Java 在应用程序启动期间如何处理org.hibernate.exception.genericjdbception?

Java 在应用程序启动期间如何处理org.hibernate.exception.genericjdbception?,java,mysql,spring,hibernate,jdbc,Java,Mysql,Spring,Hibernate,Jdbc,我正在学习hibernate/SpringMVC,启动服务器时出现了一个异常。我在intellij中工作,这个数据库连接到我的IDE并且工作得很好(同样“测试连接”也可以),但是当我试图在配置文件中创建一个bean时,它根本不工作。 有什么问题吗 @Configuration @EnableTransactionManagement @ComponentScan("com.packt.webstore") public class RootApplicationContextConfig { @

我正在学习hibernate/SpringMVC,启动服务器时出现了一个异常。我在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; 
无法打开JDBC连接以执行DDL

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

  • MySQL根本没有运行。
    • 是你开始的吗?通过检查正在运行的进程列表(取决于操作系统)进行验证
  • MySQL未在端口3306上侦听,端口3306是默认端口。
    • 你改了吗?如果是这样,请相应地更改代码/配置
  • 如果MySQL正在运行和侦听(端口3306),则用户root的密码不是
    root
    • 您是否将密码设置为
      root
      ?使用这些凭据通过mysql CLI进行本地连接。如果这样做有效,您可以排除密码不匹配的情况
  • 用户
    root
    没有权限连接到名为
    shop
    的数据库。
    • 您是否更改了用户
      root
      shop
      数据库的访问权限?如果是,请检查和/或更改此用户对本地连接的权限()
  • 希望这份清单能有所帮助

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

    • MySQL连接器/j5.x
      • jdbc.driver\u class=>
        com.mysql.jdbc.driver
    • MySQL连接器/j6.x+
      • jdbc.driver\u class=>
        com.mysql.cj.jdbc.driver

    已更改为Hibernate 5和mysql 5.1.39

    请从日志中发布完整的异常stacktrace。感谢您的提示,我刚刚编写了另一个java程序,该程序仅使用jdbc连接到具有相同设置的数据库,并且运行正常。如果这些提示对您有所帮助,并且您从中学习到,考虑给我的答案投票。不,不幸的是它仍然不在我的程序中工作,但是我确信所有的设置(密码/登录/端口等)都是正确的。这些细节在原始帖子中丢失了。你是对的,sry,我有mysql 6和hibernate 4,我认为这不会是一个问题提示:使用更新的版本5.1.46,请参见这里: