简体   繁体   中英

How to fix it: com.microsoft.sqlserver.jdbc.SQLServerException?

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

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.

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.

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

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