jdbc:jtds与SQL Server的连接 [英] jdbc:jtds Connection to SQL server

查看:442
本文介绍了jdbc:jtds与SQL Server的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JDBC驱动程序在某些Java和SQL Server之间创建连接.我创建了一个Connection类,该类应该与IP地址为"BHX"的示例计算机上的服务器建立连接.

I am trying to create a connection between some Java and a SQL Server using a JDBC driver. I have created a Connection class which should form the connection to a server which is on my computer with example IP address 'BHX'

这是Connection类

Here is the Connection class

public class Connection {

public static void main(String[] args) throws Exception {

    try {
        String databaseDriver = "net.sourceforge.jtds.jdbc.Driver";
        Class.forName(databaseDriver);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {               
        String url = "jdbc:jtds:sqlserver://BHX:1433/Forecast;instance=SQLEXPRESS";
        java.sql.Connection con =  DriverManager.getConnection(url);
        System.out.println("Connection created");
        con.close();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}
}

我想知道url字符串是否有问题,因为当我运行此代码时,出现以下错误:

I'm wondering whether there is anything wrong with the url String, as when I run this code I get the following error:

java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:417)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Connection.main(Connection.java:56)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:311)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:261)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:318)
... 5 more

我见过类似的问题(在这里像这样),但是看起来好像我已经遵循了所有可能的解决方案.我已检查是否在配置管理器中启用了TCP/IP,并且端口号是1433.

I have seen similar questions (like this here) but its looks as though I have followed all the possible solutions. I have checked that the TCP/IP are enabled in the configuration manager and also that the port number is 1433.

我尝试在禁用防火墙的情况下运行此命令,但仍然收到相同的错误.

I've tried running this with Firewalls disabled and still get the same error.

telnet BHX 1433,我收到以下消息"...无法在端口1433上打开到主机的连接:连接失败"

telnet BHX 1433 and I am getting the following message '...Could not open connection to the host, on port 1433: Connect failed'

推荐答案

当您将用户名和密码添加到连接字符串中时,它将起作用:

It will work when you add the username and password to your connection string like:

"jdbc:jtds:sqlserver://BHX:1433/Forecast;instance=SQLEXPRESS;user=XXXXX;password=XXXXX"

这应该与SQL Server身份验证一起使用.

This should work with SQL Server authentication.

如果仅使用Windows身份验证,则还需要提供以下域:

If you only use windows authentication you will also need to provide the domain like:

"jdbc:jtds:sqlserver://BHX:1433/Forecast;instance=SQLEXPRESS;domain=XXXXX;user=XXXXX;password=XXXXX"

这篇关于jdbc:jtds与SQL Server的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆