gpt4 book ai didi

java - Hibernate MS SQL 连接

转载 作者:行者123 更新时间:2023-12-04 17:17:45 25 4
gpt4 key购买 nike

我正在使用 jar jtds-1.3.0.jar 通过 hibernate 连接到 MS SQL,下面是配置文件

<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.url">jdbc:jtds:sqlserver://localhost/login</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.username">sa</property>
<property name="connection.password">user</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>


<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Echo all executed SQL to stdout -->

<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

<!-- configuration pool via c3p0-->
<property name="c3p0.acquire_increment">5</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">20</property>
<property name="c3p0.max_statements">50</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">1800</property> <!-- seconds -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!--Basic user functionality-->

</session-factory>

但每次我运行我的项目时,都会出现网络错误和连接被拒绝的错误。我推荐了this link对于仍然给出错误。下面是我的堆栈跟踪

java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:434)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:183)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:183)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:172)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:152)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1074)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1061)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1796)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:635) Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.reflect.GeneratedMethodAccessor24.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:300)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:253)
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:329)

下面是图片 Server Configuration Manager谁能告诉我哪里出错了。

最佳答案

您确定您的服务器正在监听端口 1433 吗?要确认实际问题出在 Java(即您的配置)上,请运行

telnet localhost 1433

如果您没有得到回答,那么 MS SQL 很可能没有在 1433 上运行。在 MS SQL 中有一个使用动态端口的选项,请确保您没有启用它。

http://frightanic.com/software-development/connecting-to-ms-sql-server-2012-express-through-jdbc-failed/ :

The first hurdle was to learn that MS SQL Express by default uses dynamic ports. To connect in a TCP/IP fashion from Java you need to configure static ports manually.

关于java - Hibernate MS SQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14810610/

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