简体   繁体   English

Java连接代码到SQL Server 2014

[英]Java connection code to SQL Server 2014

I'm attempting to connect to a database table on a local instance of SQL Server 2014. 我正在尝试连接到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. “与主机本地主机,端口1433的TCP / IP连接失败。错误:“连接被拒绝:连接。 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. 确保主机上正在运行SQL Server实例并在端口上接受TCP / IP连接。 Make sure that TCP connections to the port are not blocked by a firewall." 确保与端口的TCP连接没有被防火墙阻止。”

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. 我已经尝试了Internet上的所有快速修复程序,包括检查sql server服务是否正在运行,检查端口是否已被Windows防火墙阻止以及是否在SQL Server配置管理器中启用了TCP / IP连接。

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. 但是,这里的问题是,当尝试在访问特定表的此连接字符串之后执行sql语句时,它给我一个错误,即我要访问的数据库表无效。

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

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. 如果您是我,我将仔细检查您的SQL Server网络配置,以确保在端口1433上启用了TCP。

Quick google brings up this Stack Overflow link 快速谷歌提出了这个堆栈溢出链接

This link might be about 2008 but the process is the same. 该链接可能大约是2008年,但是过程是相同的。 In addition to Ed Ost instructions I would advise you check the IPAll section is set to use 1433 as well. 除了Ed Ost指令外,我建议您检查IPAll部分是否也设置为使用1433。

Your second link will work as it is using the SQL server Instance name rather than TCP. 您的第二个链接将正常使用SQL Server实例名称而不是TCP。

If you did manage to get the TCP link working you will still have to set the DB that you require though. 如果确实设法使TCP链接正常工作,则仍然需要设置所需的数据库。

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) 在连接上使用databaseName =选项(我的书中首选)

Using the JDBC Driver - Might be a good place to look. 使用JDBC驱动程序 -可能是个不错的选择。

EDIT: Ohh just seen you are using sa as your username. 编辑:哦,刚刚看到您使用sa作为您的用户名。 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. 通常这不是一个好主意,因为这是主要的sql管理员,因此存在一些怪癖和很大的安全风险。 Create a new login and map that to a user on the DB you want to use. 创建一个新的登录名并将其映射到您要使用的数据库上的用户。 For Ref - SQL server Docs 对于参考-SQL Server文档

You may need to go into SQL Server Configuration Manager and explicitly configure it to listen on 1433. 您可能需要进入SQL Server配置管理器并将其显式配置为在1433上进行侦听。

  1. Go to "SQL Server Network Configuration" 转到“ SQL Server网络配置”
  2. select "Protocols for " 选择“协议”
  3. Right-click on "TCP/IP" 右键单击“ TCP / IP”
  4. Select "Properties" 选择“属性”
  5. Select "IP Addresses" tab 选择“ IP地址”标签
  6. Make sure "TCP Port" under "IPALL" is "1433" 确保“ IPALL”下的“ TCP端口”为“ 1433”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM