简体   繁体   中英

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:

"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;" +  
     "databaseName=TestConnection;integratedSecurity=True;";
    Connection con = null;
    Statement stm = null;
    ResultSet rs = null;
    boolean check = false;
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        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;
            else
                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) {
        e.printStackTrace();
    } 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?

 String connectionUrl = "jdbc:sqlserver://localhost:2175;" + "databaseName=TestConnection;integratedSecurity=True;"; 

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

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).

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