Friday, April 14, 2023

SQL Server JDBC Error: The TCP/IP connection to the host Failed

I had installed SQL SERVER 2014 Express edition and I was trying to connect to SQL SERVER from Java program using JDBC, but I was repeatedly getting the following error:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".Error while closing connection !!null

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Testing.main(Testing.java:42)

I had checked that instance of Microsoft SQL SERVER was already running, as I was able to connect to it using SQL SERVER Management Studio (SSMS client). I had also checked the JDBC URL, it's correct and also, the JDBC driver is including in CLASSPATH. So what was wrong? Why I was getting this error? and finally, how I managed to fix this error? Read on...

By the way, if you are new to Microsoft SQL Server and T-SQL then I also suggest you join a comprehensive course to learn SQL Server fundamentals and how to work with T-SQL. If you need a recommendation then I suggest you go through the Microsoft SQL for Beginners online course by Brewster Knowlton on Udemy. It''s a great course to start with T-SQL and SQL queries in SQL Server.




Cause of error TCP/IP connection to the host Failed

If you are getting this error after installing SQL SERVER 2012 or 2014 while first time trying to connect from Java program using JDBC, then it's most likely that your SQL SERVER is not yet configured to listen for TCP/IP connection on port 1433.

If you have already configured your MSSQL server to listen on TCP/IP traffic on port 1433 then it could be any of following three reasons:
  • Firewall is blocking the incoming connection
  • SQL SERVER is not running on the host
  • The port you have configured is not correct, i.e. its not 1433

If the SQL SERVER express is not running then just start your SQL Server, you can do so by going into Windows Services tab or you can also start SQL Server from "SQL Server Configuration Manager", as shown below:

How to start SQL SERVER in Windows 8.1


By default, when you install Microsoft SQL server in Windows 8.1, it register itself as Windows service and automatically starts when you log into Windows. Btw, if you are new into JDBC world and not done much work on Java application interfacing database, then you should check out Core Java, Volume II--Advanced Features, 10th Edition by Cay S. Horstmann, one of the best books to learn advanced Java concepts.

SQL Server JDBC Error: The TCP/IP connection to the host Failed



TCP/IP connection to the host Failed: Solution

The solution is to ensure that your SQL Server instance is listening on the port. Follow these steps to ensure that SQL SERVER 2014 is start listening on port 1433:
  1. Go to Start->All Programs-> Microsoft SQL Server 2012-> Configuration Tool
  2. Click SQL Server Configuration Manager
  3. Expand SQL Server Network Configuration-> Protocol
  4. Enable TCP/IP Right box
  5. Double Click on TCP/IP and go to IP Addresses Tap and Put port 1433 under TCP port.

How to enable SQL SERVER listening on port 1433 in Windows 8.1



In Windows 8.1, you can also press Windows + c and click on Search icon to start searching for SQL Server Configuration Manager. Once found just click on it to open.

Anyway, once you got to the above screen just double click on highlighted TCP/IP link and this will open the following window, make sure you enter TCP Port as 1433. That's it.

How to configure SQL SERVER to listen on port 1433


That's all about how to fix The TCP/IP connection to the host localhost, port 1433 has failed error while connecting to SQL SERVER from Java using JDBC API. You also need to create user and logins in order to connect using JDBC and type 4 JDBC driver. I am going to post that information soon in next couple of articles.

Further Learning
JSP, Servlets and JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 and 2
Java Platform: Working with Databases Using JDBC


Related Tutorials and Guide
If you like this tutorial and wants to learn more about troubleshooting and debugging different error in Java, please see following tutorials:
  • How to fix Unsupported major.minor version 52.0 error in Java [solution]
  • Failed to connect to Queue using WebSphere MQ Series error [solution]
  • java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver [fix]
  • How to fix java.lang.classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver erorr [solution]
  • Solution of JDBC error java.lang.ClassNotFoundException: org.postgresql.Drive [solution]
  • How to deal with java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver? [solution]
  • Root cause of  java.lang.ClassNotFoundException: com.mysql.jdbc.Drive error in Java? [solution]
  • Cause and solution of java.sql.SQLException: No suitable driver : sqljdbc4.jar [solution]
  • Root cause java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in Java? [analysis]
  • Step by Step Guide to connect MySQL database using JDBC API (guide)
  • java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet [fix]
  • Java guide to connecting Oracle 10g database using JDBC thin driver (guide)


9 comments :

Anonymous said...

thanks it worked!

Wasim said...

Thanks Its working fine

jackychau said...

Thank you. its worked

javin paul said...

Hello Jackychau, glad its working for you. thx for your comment.

Curious said...

Hi, I checked for my system, its already configured as 1433, still its giving same error: Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port

javin paul said...

Hello CUrios, did you checked if SQL Server is running?

Unknown said...

thank you <3

Anonymous said...

thank you, this worked for me 👍

Anonymous said...

Not Working, Why

Post a Comment