简体   繁体   中英

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:

<!--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:

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.

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?

I would check things following these steps:

  • Ensure that SQL Server is up and running . Check your running services to see if SQL Server service is up.

  • I would check the SQL Server Configuration Manager to see if the TCP/IP communication is enabled.

  • I would check my firewall settings . Perhaps something is in the way between SQL Server and the client.

  • 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 . 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".

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
  2. Click Sql Server Network Configuration

  3. Click Protocols for SQLExpress

  4. Double click TCP/IP and click IP adrress tab

  5. Go to the end till you see IP ALL

  6. Ensure that you see port number 1433 is configured under TCP Port under IPALL header.

  7. If not configured, enter 1433 port and click Apply.

  8. You need to restart the MS sql server and try now and it should work.

Hope this will solve it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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