简体   繁体   English

连接被拒绝:Java + Hibernate中的连接错误

[英]Connection refused: connect Error in Java + Hibernate

I am trying to do this tutorial and when I got to the hibernate part, I get this errors: 我正在尝试做本教程 ,当我进入休眠部分时,出现以下错误:

java.sql.SQLException: Network error IOException: Connection refused: connect
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:436)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataS
ource.java:146)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(Wrap
perConnectionPoolDataSource.java:195)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(Wrap
perConnectionPoolDataSource.java:184)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourceP
oolManager.acquireResource(C3P0PooledConnectionPool.java:200)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.ja
va:1086)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAc
quiresWithinLockOnSuccess(BasicResourcePool.java:1073)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.j
ava:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(Basic
ResourcePool.java:1810)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolA
synchronousRunner.java:648)
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 net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.jav
a:288)
    at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
    ... 10 more

Here is the hibernate.cfg.xml part of accessing the database: 这是访问数据库的hibernate.cfg.xml部分:

<!--Database connection settings-->
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;databaseName=MyTut1db</property>
<property name="connection.username">sop1</property>
<property name="connection.password">myPW1</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

The user sop1 was created in MS SQL Server 2012 Express edition as: 用户sop1在MS SQL Server 2012 Express版本中创建为:

CREATE LOGIN sop1
    WITH PASSWORD = 'myPW1';
USE MyTut1db;
GO
CREATE USER sop1 FOR LOGIN sop1;
GO 

The database properties are in the figure below: 数据库属性如下图所示: 数据库属性

I have inabled the TCP-IP from MS SQL Server Configuration Manager (TCP Port is 1433), but the problem still persists. 我已禁用MS SQL Server配置管理器中的TCP-IP(TCP端口为1433),但问题仍然存在。

I have also closed the antivirus for a while. 我还关闭了防病毒软件一段时间。

I am asking for some help: What else can it be? 我正在寻求帮助:那还能是什么? How do I verify the user? 如何验证用户? How do I know that localhost:1433 is the right server? 我怎么知道localhost:1433是正确的服务器?

I would check things following these steps: 我将按照以下步骤检查内容:

  • Ensure that SQL Server is up and running . 确保SQL Server已启动并正在运行 Check your running services to see if SQL Server service is up. 检查您正在运行的服务,以查看SQL Server服务是否已启动。

  • I would check the SQL Server Configuration Manager to see if the TCP/IP communication is enabled. 我将检查SQL Server配置管理器,以查看是否启用了TCP / IP通信。

  • I would check my firewall settings . 我会检查防火墙设置 Perhaps something is in the way between SQL Server and the client. SQL Server和客户端之间可能正在发生某种情况。

  • I would check if another application from the same client can connect to the same server. 我将检查来自同一客户端的另一个应用程序是否可以连接到同一服务器。 Then I would check the connection string differences . 然后,我将检查连接字符串的差异

  • If all of the above were playing correctly, then I would check the privileges on the specific SQL Server, on the specific database, for the account I am trying to connect with . 如果以上所有操作均正常进行,那么我将检查与我尝试连接的帐户在特定SQL Server,特定数据库上的特权 Allow everyone to use this database as a first step to check if it is an account-permissions problem. 允许所有人使用此数据库作为检查它是否是帐户权限问题的第一步。

Hope I helped! 希望我能帮上忙!

localhost will always be the server your Java is running on - it's saying "Talk back to the same machine". localhost将始终是您的Java在其上运行的服务器-表示“与同一台计算机对话”。

If your database is hosted on a different server, you need to put that server name in the configuration. 如果您的数据库托管在其他服务器上,则需要将该服务器名称放入配置中。

Please the check the following 请检查以下

  1. Click Start >Programs -> Microsoft Sql Server 2008 R2 -> Configuration Tools -> Sql Server Configuration Manager 单击开始>程序-> Microsoft Sql Server 2008 R2->配置工具-> Sql Server配置管理器
  2. Click Sql Server Network Configuration 单击Sql Server网络配置

  3. Click Protocols for SQLExpress 单击SQLExpress的协议

  4. Double click TCP/IP and click IP adrress tab 双击“ TCP / IP”,然后单击“ IP adrress”选项卡

  5. Go to the end till you see IP ALL 直到最后看到IP ALL

  6. Ensure that you see port number 1433 is configured under TCP Port under IPALL header. 确保您看到IPALL标头下的TCP端口下配置了端口号1433。

  7. If not configured, enter 1433 port and click Apply. 如果未配置,请输入1433端口,然后单击“应用”。

  8. You need to restart the MS sql server and try now and it should work. 您需要重新启动MS sql服务器并立即尝试,它应该可以工作。

Hope this will solve it. 希望这能解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM