Java Hibernate无法连接到MySQL数据库

时间:2016-04-24 17:46:00

标签: java mysql hibernate

我正在尝试用Hibernate 5构建一个项目。

当我测试连接时,它失败了......有人可以帮助我吗?

hibernate.cfg.xml中:

gettext

getSessionFactory方法:

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://IP_ADDRESS</property>
    <property name="hibernate.connection.username">USER</property>
    <property name="hibernate.connection.password">PASSWORD</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.connection.pool_size">30</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>

    <mapping class="classPath"></mapping>

</session-factory>

错误是:

public static SessionFactory getSessionFactory() throws DataException {
    if (sessionFactory == null) {
        synchronized (SessionFactory.class) {
               if (sessionFactory == null) {
                try {
                      Configuration cConfig = new Configuration();

                    if (hibernateConfig == null)
                        cConfig.configure();
                    else
                        cConfig.configure(hibernateConfig);
                    sessionFactory = cConfig.buildSessionFactory();
                } catch (Throwable ex) {
                    ex.printStackTrace();
                    throw new DataException(
                            DataException.GENERAL_ERROR_CODE,
                            "Failed to create sessionFactory object", ex);
                }
            } else {
                return sessionFactory;
            }
        }
    }
    return sessionFactory;
}

为什么我的连接失败,如何解决?

2 个答案:

答案 0 :(得分:0)

    public class HibernateUtil
{

    private static final SessionFactory sessionFactory = buildSessionFactory();

   private static SessionFactory buildSessionFactory(){
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml)
            // config file.
            //Annotation
            Configuration cfg = new Configuration();
            SessionFactory sf = (SessionFactory) cfg.configure().buildSessionFactory();
            return sf;

        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }
}

答案 1 :(得分:0)

我会发表评论,但由于重复点而无法发表评论。很抱歉给您带来不便

由于此行可能会发生此错误。

<property name="hibernate.connection.url">jdbc:mysql://IP_ADDRESS</property>

您是否在IP_ADDRESS之后放置了架构名称?像这样的东西;

jdbc:mysql://localhost:3306/myDb