有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用jdbc的java TCP/IP连接问题。数据库

我正在尝试连接到网络上计算机上的SQL数据库。据我所知,TCP/IP设置和防火墙设置都是正确的,可以连接,但我没有我的连接字符串运气

代码:

static String dbUser = "user";
static String dbPass = "pass";
static String dbURL = "jdbc:sqlserver://SERVER1/BKUPEXEC;Instance=SQL2005;databaseName=images;";
static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

try {
        Class.forName(driverName).newInstance();
        con = DriverManager.getConnection(dbURL, dbUser, dbPass);
        System.out.println("Connected with host:port/database.");
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException |    SQLException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

错误:

SEVERE: null
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host TMTSERVER1, port 1433 has failed. Error: "connect timed out. 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.".

共 (2) 个答案

  1. # 1 楼答案

    我最终改变了连接字符串,使用服务器的IP地址而不是域名。还发现SQL server没有使用默认端口号

    这是新的工作连接字符串:

    static String dbURL = "jdbc:sqlserver://192.168.1.240:1053;databaseName=SI_images;";
    
  2. # 2 楼答案

    试试看

    "jdbc:sqlserver://SERVER1/BKUPEXEC;instanceName=SQL2005;databaseName=images;"
                                       ^^^^^^^^^^^^