从 Java 连接到 SQL Server 2008
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2606653/
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
Connecting to SQL Server 2008 from Java
提问by Xinus
I am trying to connect to SQL Server 2008 server from Java
我正在尝试从 Java 连接到 SQL Server 2008 服务器
here is a program
这是一个程序
import java.sql.*;
public class connectURL {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://localhost/SQLEXPRESS/Databases/HelloWorld:1433;";// +
//"databaseName=HelloWorld;integratedSecurity=true;";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}
But it shows error as
但它显示错误为
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/SQLEXPRESS/Databases/HelloWorld, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at connectURL.main(connectURL.java:43)
I have given followed all the instructions as given in http://teamtutorials.com/database-tutorials/configuring-and-creating-a-database-in-ms-sql-2008
我已经按照http://teamtutorials.com/database-tutorials/configuring-and-creating-a-database-in-ms-sql-2008 中给出的所有说明进行操作
What can be the problem ?
可能是什么问题?
采纳答案by Venkat
I thing the connection URL may be wrong. Then try following connection,
我认为连接 URL 可能是错误的。然后尝试以下连接,
String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=YourDBName;user=UserName;Password=YourPassword"
回答by fish
Do you have localhost defined in your hosts file? Try replacing localhost with 127.0.0.1 in the connection url.
您的主机文件中是否定义了 localhost?尝试在连接 url 中用 127.0.0.1 替换 localhost。
...and you have the SQL Server running in the same computer, right?
...并且您在同一台计算机上运行 SQL Server,对吗?
回答by Joey Gibson
TCP/IP connections are disabled by default when you install the Express version of SQL Server. You need to run the SQL Server Configuration Manager and turn on TCP/IP. You also need to set the port it's listening on to 1433.
安装 SQL Server 的 Express 版本时,默认情况下禁用 TCP/IP 连接。您需要运行 SQL Server 配置管理器并打开 TCP/IP。您还需要将其侦听的端口设置为 1433。