I want to connect java class files with sql server 2012 I've logged in with sql server authentication But i'm receiving an error in connectivity

ERROR: The TCP/IP connection to the host 127.0.0.1, 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 code ---

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  //1. Register your driver
//2. get the connection object
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=aysha","sa","admin");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;databaseName=aysha","user=sa","password=admin");
 //"jdbc:sqlserver://127.0.0.1:1433; Instance=SQL2008;" +       "databaseName=MB;user=sa;password=123;";
//Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=aysha","sa" , "password");
//3. Prepare a statement
Statement stmt = con.createStatement();
//4. Write the query`
String sql = "Select * from employee";
//5. Execute the statement and
ResultSet rs = stmt.executeQuery(sql);
//6. Process the result set

while (rs.next())
{
    System.out.println(rs.getInt(1));
}
Best Answer


  1. Open sql server configuration manager, and then expand sql server 2012 network configuration.
  2. Click protocols for instancename, and then make sure tcp/ip is enabled in the right panel and double-click tcp/ip.
  3. Please note the value on the tab protocol
  4. Click the IP Addresses tab: If the value of Listen All is yes, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item under IPAll. If the value of Listen All is no, the TCP/IP port number for this instance of SQL Server 2012 is the value of the TCP Dynamic Ports item for a specific IP address.
  5. Make sure the tcp port is 1433
  6. Click ok.

If you have questions please let me know

Thank you