Full error I'm getting:
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.".
I have already checked that TCP/IP is enabled, using port 1433, and TCP dynamic ports is empty. I have disabled windows firewall.
Here is my code:
import java.sql.*;
public class DBConnect {
public static void main(String[] args) {
// TODO Auto-generated method stub
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=TestDB1;instance=SQLSERVER;encrypt=true;TrustServerCertificate=true;";
String user = "sa";
String pass = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myConn = DriverManager.getConnection(dbURL, user, pass);
try {
Statement myStmt = myConn.createStatement();
try {
ResultSet myRs = myStmt.executeQuery("Select * from Login");
while (myRs.next())
{
System.out.println(myRs.getString("Username"));
System.out.println(myRs.getString("Password"));
}
}
catch (Exception e)
{
System.out.println("Error with query");
}
}
catch (Exception e)
{
System.out.println("Error connecting to database");
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
If the Microsoft Windows registry TcpPort key is not using the default SQL Server port of 1433, test the JDBC connection string using the port set in the TcpPort registry key. If test connection still fails, disable any firewall software or ensure the firewall has a rule to allow communication on the port being used.
Double click on TPC/IP which is on left panel. On Protocol tab if Enabled is No Then Make Yes. Then click on IP Address tab. Scroll down and last IPALL, set TCP Port as 1433.
Port 1433 disabled on the MS-SQL Server In previous versions of MS-SQL server, port 1433 was enabled (open) by default, but as a hardening procedure implemented to improve security of the MS-SQL server, port 1433 is disabled in current versions of MS-SQL Server.
Have you enabled 'Named Pipes' and 'TCP/IP'?
Open the 'Sql Server Configuration' application.
In the left pane, go to 'SQL Server Network Configuration' -> 'Protocols for [instance-name]'
Right-click on both 'Named Pipes' and 'TCP/IP' and select 'enable'.
Have you used the correct port?
Double-click on 'TCP/IP'
Select 'IP Addresses' tab
Scroll to IPAII. Your port number is here.
This error usually come when SQL server not accepting TCP/IP Connection, pls try below steps it will work for sure.
1)open run and add command SQLServerManager15.msc 2)click on network configuration then "protocols for MSSQLSERVER" 3)Select protocol name - "TCP\IP" and make sure that it is enable if not then pls make it enable. 4)Check the property and find port in IP address tab.
Restart the server, it should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With