简体   繁体   中英

Java connection code to SQL Server 2014

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.

由于这是本地主机,您可以尝试以下方法:

Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost;user=sa;password=secret");

If I was you I would double check your SQL server network configuration to make sure that TCP is enabled on port 1433.

Quick google brings up this Stack Overflow link

This link might be about 2008 but the process is the same. In addition to Ed Ost instructions I would advise you check the IPAll section is set to use 1433 as well.

Your second link will work as it is using the SQL server Instance name rather than TCP.

If you did manage to get the TCP link working you will still have to set the DB that you require though.

There are two ways of doing this in my experience.

  1. Set the user to have a default database
  2. Use the databaseName= option on the connection (Preferred in my book)

Using the JDBC Driver - Might be a good place to look.

EDIT: Ohh just seen you are using sa as your username. This isn't generally a good idea, as this is the main sql administrator, and as such has some quirks and a big security risk. Create a new login and map that to a user on the DB you want to use. For Ref - SQL server Docs

You may need to go into SQL Server Configuration Manager and explicitly configure it to listen on 1433.

  1. Go to "SQL Server Network Configuration"
  2. select "Protocols for "
  3. Right-click on "TCP/IP"
  4. Select "Properties"
  5. Select "IP Addresses" tab
  6. Make sure "TCP Port" under "IPALL" is "1433"

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