简体   繁体   English

与SQL Server的Java连接

[英]Java connection to sql server

Ok so I have this code: 好的,所以我有这段代码:

package com.andrewxd.banksystem;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Interface 
    {
        public static void main(String[] args)
        {
            try
            {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
                Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1443;user=Andrew;password=andrei23;database=BankSystem");
                System.out.println("test");
                Statement sta = conn.createStatement();
                String Sql = "select * from Clients";
                ResultSet rs = sta.executeQuery(Sql);

                System.out.println(rs.next());
            } catch (Exception e){
                e.printStackTrace();
            }
    }


}

but it gives me this error can somebody help me?: 但这给了我这个错误,有人可以帮我吗?:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1443 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've tried searching but I didn't really understand much. 我曾尝试搜索,但并没有真正了解。

from what i understand the port is incorrect but how do i find the correct ip/port? 根据我的理解,端口不正确,但是如何找到正确的IP /端口?

1) Open SQL Server configuration manager and check to see if the TCP/IP under Network configuration protocols is enabled. 1)打开SQL Server配置管理器,并检查是否启用了网络配置协议下的TCP / IP。

2) Under properties of the SQL serve under Connections check to see if Allow remote connections to this server is allowed. 2)在SQL的“属性”下,在“连接”下进行检查,以查看是否允许“允许对此服务器的远程连接”。

3) Check to see if you can connect via SSMS and query the database. 3)检查是否可以通过SSMS连接并查询数据库。

4) In SQL Server Configuration manager check to see if the SQL Server Browser service is running. 4)在SQL Server配置管理器中,检查SQL Server浏览器服务是否正在运行。 (this is not enabled by default nor is it set up to start up automatically by default). (默认情况下未启用此功能,默认情况下也未设置为自动启动)。

5) If all those are set up then I would check the firewall. 5)如果所有这些都已设置,那么我将检查防火墙。

(For anyone that might come across this the solution was to allow SQL Server and windows authentication) (对于可能遇到此问题的任何人,解决方案是允许SQL Server和Windows身份验证)

Make sure your sql server configuration -has TCP configured, and configured at that IP address. 确保您的sql server配置-已配置TCP,并已在该IP地址配置。

You can test a couple things: 您可以测试几件事:

  1. turn off Windows Firewall, from services.msc. 从services.msc关闭Windows防火墙。
  2. try connecting to SQL Server through the IP address,username,pwd the application is trying to use. 尝试通过应用程序尝试使用的IP地址,用户名和密码连接到SQL Server。 (open enterprise manager, connect, enter IP address etc) (打开企业管理器,连接,输入IP地址等)

尝试将jtds驱动程序用于SQLServer。

Installing Oracle JDBC driver from Here 此处安装Oracle JDBC驱动程序

And with your code looking like this: 并且您的代码如下所示:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class OracleJDBC {

    public static void main(String[] argv) {

        System.out.println("-------- Oracle JDBC Connection Testing ------");

        try {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle JDBC Driver Registered!");

        Connection connection = null;

        try {

            connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:BankSystem", "Andrew","andrei23");

            } 
        catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }

}

It should probably work, if not, what does the output look like now ? 如果没有,它应该应该可以工作,现在的输出是什么样的? Still equal to what it was before ? 仍然等于以前的水平吗? Did you check firewalls ? 您检查防火墙了吗? Connectivity to server allowed ? 允许连接到服务器?

就我所知,sqlserver的标准端口是1433,而不是1443。

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

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