SQL server connection refused when trying to connect to DB through a remote computer

I’m new to programming and I was making this chat server with login screen that extracts data from SQL server. It seemed fine when tested it in my laptop (which is where the database is). But when I tried to use to run the code on another desktop,

it kept saying:

  • Recognize application connected to SQL Server 2005
  • What are the differences between Merge Join and Lookup transformations in SSIS?
  • DISTINCT for only one Column
  • Auto number fields
  • Copyable Coldfusion SQL Exception
  • Stored Procedure generator tool
  • “TCP/IP connection to host localhost, port 2175 has failed …”

    I have tried doing these so far:

    • enable TCP/IP protocols
    • set port to 1433
    • set dynamic port to 2175
    • run the SQL Browser (set “Local service” in Built-in Account section)
    • restart the SQL Server (set “Network service” in Built-in Account section)
    • create a bunch of TCP port rules (both Inbound and Outbound – Allow all)
    • create sqlserver program rules (both Inbound and Outbound – Allow all)
    • add sqlserver in firewall exception

    Yet it doesn’t seem to have any effect.

    This is my code to connect to SQLserver:

    public boolean connect(String SQL) {
        String connectionUrl = "jdbc:sqlserver://localhost:2175;" +  
        Connection con = null;
        Statement stm = null;
        ResultSet rs = null;
        boolean check = false;
        try {
            con = DriverManager.getConnection(connectionUrl);
            stm = con.createStatement();
            if (SQL.contains("update") || SQL.contains("insert") || SQL.contains("delete") || SQL.contains("UPDATE") || SQL.contains("INSERT") || SQL.contains("DELETE")) {
                int clu = stm.executeUpdate(SQL);
                if (clu == 0)
                    check = false;
                    check = true;
            else {
                rs = stm.executeQuery(SQL);
                while (rs.next()) {
                    System.out.println(rs.getString(1) + " | " + rs.getString(2));
                    check = true;
        } catch (ClassNotFoundException | SQLException e) {
        } finally {  
            if (rs != null) try { rs.close(); } catch(Exception e) {}  
            if (stm != null) try { stm.close(); } catch(Exception e) {}  
            if (con != null) try { con.close(); } catch(Exception e) {}  
        return check;

    I’m using SQL server 2016. I know there’re tons of topics discussing about this problem but I can’t seem to find the solution.

    I also need to mention: the desktop I’m trying to run my code on doesn’t have SQL server installed. Does it need to?

  • Different representation of UUID in Java Hibernate and SQL Server
  • How to connect Android Studio with SQL Server database
  • how to add sqljdbc_auth.dll to my maven project
  • Unable to execute stored Procedure using Java and JDBC on SQL server
  • JPA GenerationType.AUTO not considering column with auto increment
  • JDBC connection hangs with no response from SQL Server 2008 r2
  • One Solution collect form web for “SQL server connection refused when trying to connect to DB through a remote computer”

    String connectionUrl = “jdbc:sqlserver://localhost:2175;” +

    Above code means, you are trying to connect to database (i.e. localhost), which is installed on same machine.

    Now, if you are trying to connect to remote server, then you need to use its IP address instead. Also, you need to make sure they are in same network (try pinging that machine using its IP adddress).

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.