使用hibernate连接到数据库 - 连接URL

时间:2015-04-22 14:09:22

标签: java hibernate jdbc sql-server-2014

我正在尝试使用hibernate连接到数据库,我已经阅读了文档和用户公会,但我仍然无法连接到数据库。 我采取的步骤:

  1. 将所有相关的hibernate jar和rules = [] dirnames = args["directories"]["dirname"] if isinstance(dirnames, basestring): rules.append(os.path.join(dirnames, "*", "*")) elif isinstance(dirnames, list): for v in dirnames: rules.append(os.path.join(v, "*", "*")) (sql server jdbc driver)添加到我的项目中。
  2. 将带有以下sqljdbc41.jar(x64)的xml添加到环境路径。
  3. 使用以下连接和方言创建名称为sqljdbc_auth.dll的xml:

    hibernate.cfg.xml
  4. 因为你可以看到我的服务器是本地的,我正在使用Windows身份验证,我也没有更改defualt实例和端口。

    我尝试使用以下代码连接到数据库:

        <!-- Database connection settings -->
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">"jdbc:sqlserver://localhost;databaseName=ForumSystem;integratedSecurity=true;"</property>
    
    <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    

    我收到以下错误:

     Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    

    我试图修改连接url以包含端口和实例名称,并尝试连接而不进行Windows身份验证但没有任何进展。

    我错过了什么吗?有没有办法知道哪个参数是错误的?

    编辑:忘记提及在SQL Server配置中启用了TCP \ IP,并且我允许在数据库中进行Windows身份验证和SQL身份验证。 如果我尝试连接使用:

    Exception in thread "main" org.hibernate.HibernateException: Unable to make JDBC Connection ["jdbc:sqlserver://SHPERB;databaseName=ForumSystem;integratedSecurity=true;"]
    

    它可以工作,但不使用hibernate

1 个答案:

答案 0 :(得分:0)

尝试在您的网址中添加jdbc:microsoft:sqlserver。你需要为Microsoft SQL JDBC Driver指定驱动程序。看更多 Connecting to MS sql through hibernatehow to configure hibernate config file for sql server

同时检查是否设置了自定义端口并将其添加到连接参数中。