Java 创建 jTDS 连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1862283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a jTDS connection string
提问by Omu
my sql server instance name is MYPC\SQLEXPRESS and I'm trying to create a jTDS connection string to connect to the database 'Blog'. Can anyone please help me accomplish that?
我的 sql 服务器实例名称是 MYPC\SQLEXPRESS,我正在尝试创建一个 jTDS 连接字符串来连接到数据库“博客”。任何人都可以帮我完成那个吗?
I'm trying to do like this:
我正在尝试这样做:
DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/Blog", "user", "password");
and I get this:
我明白了:
java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:395)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at SqlConnection.Connect(SqlConnection.java:19)
at main.main(main.java:11)
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:305)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:255)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:323)
... 6 more
采纳答案by Pascal Thivent
As detailed in the jTDS Frequenlty Asked Questions, the URL format for jTDS is:
如 jTDS Frequenlty Asked Questions中所述,jTDS的 URL 格式为:
jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]
So, to connect to a database called "Blog" hosted by a MS SQL Server running on MYPC
, you may end up with something like this:
因此,要连接到由运行在 上的 MS SQL Server 托管的名为“博客”的数据库MYPC
,您可能会得到如下结果:
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS;user=sa;password=s3cr3t
Or, if you prefer to use getConnection(url, "sa", "s3cr3t")
:
或者,如果您更喜欢使用getConnection(url, "sa", "s3cr3t")
:
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS
EDIT:Regarding your Connection refused
error, double check that you're running SQL Server on port 1433, that the service is running and that you don't have a firewall blocking incoming connections.
编辑:关于您的Connection refused
错误,请仔细检查您是否在端口 1433 上运行 SQL Server,该服务是否正在运行,并且您没有阻止传入连接的防火墙。
回答by Courtney Faulkner
jdbc:jtds:sqlserver://x.x.x.x/database
replacing x.x.x.x
with the IP or hostname of your SQL Server machine.
jdbc:jtds:sqlserver://x.x.x.x/database
替换x.x.x.x
为您的 SQL Server 计算机的 IP 或主机名。
jdbc:jtds:sqlserver://MYPC/Blog;instance=SQLEXPRESS
jdbc:jtds:sqlserver://MYPC/Blog;instance=SQLEXPRESS
or
或者
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS
jdbc:jtds:sqlserver://MYPC:1433/Blog;instance=SQLEXPRESS
If you are wanting to set the username and password in the connection string too instead of against a connection object separately:
如果您也想在连接字符串中设置用户名和密码,而不是单独针对连接对象:
jdbc:jtds:sqlserver://MYPC/Blog;instance=SQLEXPRESS;user=foo;password=bar
jdbc:jtds:sqlserver://MYPC/Blog;instance=SQLEXPRESS;user=foo;password=bar
(Updated my incorrect information and add reference to the instance syntax)
(更新了我的错误信息并添加了对实例语法的引用)
回答by Pratik Bhatt
A shot in the dark, but From the looks of your error message, it seems that either the sqlserver instance is not running on port 1433 or something is blocking the requests to that port
黑暗中的一个镜头,但从您的错误消息的外观来看,似乎 sqlserver 实例没有在端口 1433 上运行,或者某些东西阻止了对该端口的请求
回答by acdcjunior
Really, really, really check if the TCP/IP protocol is enabled in your local SQLEXPRESS instance.
真的,真的,真的要检查一下本地 SQLEXPRESS 实例中是否启用了 TCP/IP 协议。
Follow these steps to make sure:
请按照以下步骤确保:
- Open "Sql Server Configuration Manager" in "Start Menu\Programs\Microsoft SQL Server 2012\Configuration Tools\"
- Expand "SQL Server Network Configuration"
- Go in "Protocols for SQLEXPRESS"
- Enable TCP/IP
- 在“开始菜单\程序\Microsoft SQL Server 2012\配置工具\”中打开“ Sql Server 配置管理器”
- 展开“SQL Server 网络配置”
- 进入“SQLEXPRESS 的协议”
- 启用 TCP/IP
If you have any problem, check this blog postfor details, as it contains screenshots and much more info.
如果您有任何问题,请查看此博客文章了解详细信息,因为它包含屏幕截图和更多信息。
Also check if the "SQL Server Browser" windows service is activated and running:
还要检查“SQL Server Browser”Windows 服务是否已激活并正在运行:
- Go to Control Panel -> Administrative Tools -> Services
- Open "SQL Server Browser" service and enable it (make it manual or automatic, depends on your needs)
- Start it.
- 进入控制面板 -> 管理工具 -> 服务
- 打开“SQL Server Browser”服务并启用它(手动或自动,取决于您的需要)
- 启动它。
That's it.
就是这样。
After I installed a fresh local SQLExpress, all I had to do was to enable TCP/IP and start the SQL Server Browser service.
在我安装了一个全新的本地 SQLExpress 之后,我所要做的就是启用 TCP/IP 并启动 SQL Server Browser 服务。
Below a code I use to test the SQLEXPRESS local connection. Of course, you should change the IP, DatabaseName and user/password as needed.:
下面是我用来测试 SQLEXPRESS 本地连接的代码。当然,您应该根据需要更改 IP、DatabaseName 和用户/密码。:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JtdsSqlExpressInstanceConnect {
public static void main(String[] args) throws SQLException {
Connection conn = null;
ResultSet rs = null;
String url = "jdbc:jtds:sqlserver://127.0.0.1;instance=SQLEXPRESS;DatabaseName=master";
String driver = "net.sourceforge.jtds.jdbc.Driver";
String userName = "user";
String password = "password";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected to the database!!! Getting table list...");
DatabaseMetaData dbm = conn.getMetaData();
rs = dbm.getTables(null, null, "%", new String[] { "TABLE" });
while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); }
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
rs.close();
}
}
}
And if you use Maven, add this to your pom.xml:
如果您使用 Maven,请将其添加到您的 pom.xml 中:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
</dependency>
回答by Nelda.techspiress
SQLServer runs the default instance over port 1433. If you specify the port as port 1433, SQLServer will only look for the default instance. The name of the default instance was created at setup and usually is SQLEXPRESSxxx_xx_ENU.
SQLServer 通过端口 1433 运行默认实例。如果将端口指定为端口 1433,SQLServer 将只查找默认实例。默认实例的名称是在安装时创建的,通常是SQLEXPRESSxxx_xx_ENU。
The instance name also matches the folder name created in Program Files -> Microsoft SQL Server. So if you look there and see one folder named SQLEXPRESSxxx_xx_ENU it is the default instance.
实例名称也与在 Program Files -> Microsoft SQL Server 中创建的文件夹名称相匹配。因此,如果您查看那里并看到一个名为 SQLEXPRESSxxx_xx_ENU 的文件夹,它就是默认实例。
Folders named MSSQL12.myInstanceName(for SQLServer 2012) are named instances in SQL Server and are notaccessed via port 1433.
命名的文件夹MSSQL12.myInstanceName(对于SQLServer的2012)被命名为SQL Server实例,并且不通过端口1433进行访问。
So if your program is accessing a default instance in the database, specify port 1433, and you may not need to specify the instance name.
因此,如果您的程序正在访问数据库中的默认实例,请指定端口 1433,您可能不需要指定实例名称。
If your program is accessing a named instance (not the default instance) in the database DO NOTspecify the port but you must specify the instance name.
如果您的程序正在访问数据库中的命名实例(不是默认实例),请不要指定端口,但必须指定实例名称。
I hope this clarifies some of the confusion emanating from the errors above.
我希望这可以澄清上述错误引起的一些混乱。