【问题标题】:How to connect with hibernate file to SQL server database?如何将休眠文件连接到 SQL Server 数据库?
【发布时间】:2021-05-06 14:25:24
【问题描述】:

我有一个问题,因为我不知道如何使用休眠文件连接到 SQL Server 我试试这个

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database Connection settings -->
        <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=RentApp</property>
        <property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
        <property name="hibernate.connection.password"></property>

        <!-- Echo SQL wyswietlenie zapytania tabeli bazy danych -->
        <property name="hibernate.show_sql">true</property>

        <!-- SQL Dialect implementacja silnika bazo danego  -->
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

        <!-- database intialize -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- mappings -->
        <mapping class="pl.edu.wszib.model.Vehicle" />
        <mapping class="pl.edu.wszib.model.User" />
        <mapping class="pl.edu.wszib.model.GroupVehicle" />
        <mapping class="pl.edu.wszib.model.Rent" />
        <mapping class="pl.edu.wszib.model.Buy" />
    </session-factory>
</hibernate-configuration>

在 SQL Server 中,我的数据库名称是 RentApp,我在 Microsoft SQL Server 中使用 Windows 身份验证。 所以

 <property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
        <property name="hibernate.connection.password"></property>

用户名必须来自 Microsoft SQL Server? 我在我的 pom 中使用

  <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.2.2.jre11</version>
        </dependency>

我有一个错误,但我有用户名:LAPTOP-SJJ2BT5V\Konrad

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'LAPTOP-SJJ2BT5V\Konrad'. 

我试试这个

 <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>

但我有错误:

com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication.

【问题讨论】:

    标签: java sql-server hibernate


    【解决方案1】:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <!-- Database Connection settings -->
            <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=Test;integratedSecurity=true</property>
            <property name="hibernate.connection.username">LAPTOP-SJJ2BT5V\Konrad</property>
            <property name="hibernate.connection.password"></property>
    
            <!-- Echo SQL wyswietlenie zapytania tabeli bazy danych -->
            <property name="hibernate.show_sql">true</property>
    
            <!-- SQL Dialect implementacja silnika bazo danego  -->
            <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    
            <!-- database intialize -->
            <property name="hibernate.hbm2ddl.auto">update</property>
    
            <!-- mappings -->
            <mapping class="pl.edu.wszib.model.Vehicle" />
            <mapping class="pl.edu.wszib.model.User" />
            <mapping class="pl.edu.wszib.model.GroupVehicle" />
            <mapping class="pl.edu.wszib.model.Rent" />
            <mapping class="pl.edu.wszib.model.Buy" />
        </session-factory>
    </hibernate-configuration>
    

    我必须下载 sqljdbc_auth.dll 并将这个文件放到 C:\Program Files\Java\jdk-11.0.1\bin

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多