gpt4 book ai didi

java - 使用 hibernate 连接到 SQL Server 2017

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:06 24 4
gpt4 key购买 nike

我尝试使用 hibernate 连接到 SQL Server,但收到此错误:

sourceId=null[severity=(ERROR 2), summary=(Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]), detail=(Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment])]

以下是我的 persistence.xml

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:jtds:sqlserver://localhost:1433/[databaseName]?createDatabaseIfNotExist=true&amp;useSSl=false&amp;autoReconnect=true"/>
<property name="javax.persistence.jdbc.user" value="***"/>
<property name="javax.persistence.jdbc.password" value="***"/>
<property name="javax.persistence.jdbc.driver" value="net.sourceforge.jtds.jdbc.Driver"/>

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>

</properties>

和 pom.xml

    <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
</dependency>

编辑

我将此依赖项添加到我的 pom

    <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.0.Final</version>
</dependency>

现在错误消息已更改。

WARN: HHH000342: Could not obtain connection to query metadata : null
dez 12, 2019 10:40:37 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
dez 12, 2019 10:40:37 AM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was null
dez 12, 2019 10:40:37 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08S01
dez 12, 2019 10:40:37 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Falha na conexão TCP/IP com o host xxx.xx.xx.xx/processos_corretivos?createDatabaseIfNotExist=true&useSSl=false&autoReconnect=true, porta 1433. Erro: "xxx.xx.xx.xx/processos_corretivos?createDatabaseIfNotExist=true&useSSl=false&autoReconnect=true. **Verifique as propriedades da conexão. Verifique se uma instância do SQL Server está sendo executada no host e se está aceitando conexões TCP/IP na porta. Verifique se as conexões TCP na porta não foram bloqueadas por um firewall**.".

检查连接属性。检查主机中是否有正在执行的 SQL Server 实例以及它是否接受 TPC/IP 连接。检查 TCP 连接是否未被防火墙阻止。

hibernate 只连接端口吗?我可以在没有端口的情况下连接到 MSSMS 中的数据库,但如果我使用端口,则无法连接。

最佳答案

我通过更改网址修复了它。

<property name="javax.persistence.jdbc.url"
value="jdbc:sqlserver://xxx.xx.xx.xx;databaseName=processos_corretivos"/>

关于java - 使用 hibernate 连接到 SQL Server 2017,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59292427/

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