I'm attempting to connect to a database table on a local instance of SQL Server 2014.
The error that I am receiving is
"The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. 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."
My connection code is below.
public static Connection ConnectDB() {
try {
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;user=sa;password=secret");
System.out.println("Connected");
return conn;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
System.out.println("Database connection error.");
return null;
}
}
I've tried all the quickfixes on the internet, including checking that the sql server services are running, checking if the port has been blocked by windows firewall, and that the TCP/IP connections are enabled in SQL server config manager.
Is there anything else I can try to fix this?
I tried the following connection string, which connected successfully. However it doesn't connect me to the database which I need to work with.
String dbURL = "jdbc:sqlserver://localhost\\sqlexpress;user=sa;password=secret";
The issue here, though, is that when trying to execute an sql statement after this connection string, which accesses a specific table, it gives me an error that the database table I'm trying to access is invalid.