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?