简体   繁体   中英

Cannot connect to SQL Server from Java

I have a problem when I try to connect to SQL Server with Java. I have the next error:

The connection to the host DESKTOP-C0SCI39, named instance failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.

My firewall is off and I don't know what is wrong(mssql-jdbc-8.4.1.jre14 is included). This is my Java code:

String url = "jdbc:sqlserver://DESKTOP-C0SCI39\\;databaseName=students";
        try {
            Connection connection = DriverManager.getConnection(url);
            System.out.println("Connected to MS SQL");
        } catch (SQLException throwables) {
            System.out.println("Error!");
            throwables.printStackTrace();
        }

Try this solution, I have ran into similar problems in the past and have found that these help.

  1. Check that the JDBC Driver is imported in the project. (Preferred - 5.1.48)

  2. Check that your SQL Server is up and running (Check if SQL Service is running)

  3. Check if you entered your connection string properly

     eg: private String URL = "jdbc:mysql://localhost:3306/dbName"; //database url
  4. Try adding a useSSL=false to the end of your connection string dbName?useSSL=false

  5. Open your server with a server management software such as SQL Server Management Studio to see if your SQL Server is working as it should

Your database is not running where you think it is running. Verify the port and location of the server and try again.

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