0

I want to connect to sql server express. I downloaded this driver. I read the help file and this is my code:

        try 
        {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://localhost;database=ActorsDb;integratedSecurity=true;";
            Connection con = DriverManager.getConnection(connectionUrl);
            PreparedStatement st = con.prepareStatement("INSERT INTO Actors(FirstName,LastName,Age) VALUES(?,?,?)");
            st.setString(1, "Robert");
            st.setString(2, "de Niro");
            st.setInt(3,45);
            st.executeUpdate();     
            con.close();
        } 
        catch (SQLException | ClassNotFoundException e) 
        {
            e.printStackTrace();
        }   

I get this exception:com.microsoft.sqlserver.jdbc.SQLServerException: 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.".

I turned off firewall but nothing changed.

  1. I went to SQL Server Configuration Manager and i enabled TCP/IP

  2. I went to IP Adresses, IP1 and set the properties

Active:Yes ; Enabled:Yes ; TCP Dynamic Ports:[empty] ; TCP Port:1433

Any tips of what i am missing? Thanks.

Jack Willson
  • 2,094
  • 6
  • 21
  • 23

3 Answers3

0

Check if the SQL Server service is running and check if any programs like anti-virus is blocking the connections. If so, disable them or add exception to that connection.

Amith Koujalgi
  • 10,746
  • 2
  • 18
  • 22
0

You're using localhost in your JDBC URL, try instead using the IP address for "IP1" that you describe in SSCM. Or, verify SSCM is configured to listen on localhost:1433.

ty733420
  • 894
  • 7
  • 13
0

Problem solved. I had to copy sqljdbc_auth.dll in the working directory.

Jack Willson
  • 2,094
  • 6
  • 21
  • 23
  • i am trying to connect sql server with hive using sqoop `[root@SLAVENODE1 sqoop/bin]# sqoop import --connect "jdbc:sqlserver://10.23.52.15:1433;dtabase=test;username=****;password=****" --table dept -m 1 --hive-import -- --schema dbo`, the above same error occurs. I moved sqljdbc4.jar in sqoop/lib folder. As you mentioned where to copy sqljdbc_auth.dll . Kindly help – Arun Gunalan Aug 26 '16 at 08:08