JAVA连接MSSQL2005的出错与解决(附代码)
2009-06-10 17:07
357 查看
在 SQL Server 2005 里面,如果出现到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect!
估计是因为sqlserver2005默认情况下是禁用了tcp/ip连接。
您可以在命令行输入:telnet localhost 1433进行检查,这时会报错:正在连接到localhost...不能打开到主机的连接,在端口 1433: 连接失败 启动tcp/ip连接的方法:
打开 /Microsoft SQL Server 2005/配置工具/目录下的SQL Server Configuration Manager,选择mssqlserver协议, 然后右边窗口有个tcp/ip协议,然后启动它,把sqlserver服务停了,然后在启动。问题就解决了! 这时在命令行输入:telnet localhost 1433就不会再报错了,窗口显示为一片黑,即为正常。
代码:
//=====================================================================
//
// File: connectURL.java
// Summary: This Microsoft SQL Server 2005 JDBC Driver sample application
// demonstrates how to connect to a SQL Server database by using
// a connection URL. It also demonstrates how to retrieve data
// from a SQL Server database by using an SQL statement.
// Date: April 2006
//
//---------------------------------------------------------------------
//
// This file is part of the Microsoft SQL Server JDBC Driver Code Samples.
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//=====================================================================
package mssql;
4000
import java.sql.*;
public class connectURL {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://localhost:2044;" + //主机名和加端口号,默认是1433.
"user=nono;password=nono123"; //用户名和密码。。。应该不用说了吧
// 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 * from test";
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(1));
//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) {}
}
}
}
估计是因为sqlserver2005默认情况下是禁用了tcp/ip连接。
您可以在命令行输入:telnet localhost 1433进行检查,这时会报错:正在连接到localhost...不能打开到主机的连接,在端口 1433: 连接失败 启动tcp/ip连接的方法:
打开 /Microsoft SQL Server 2005/配置工具/目录下的SQL Server Configuration Manager,选择mssqlserver协议, 然后右边窗口有个tcp/ip协议,然后启动它,把sqlserver服务停了,然后在启动。问题就解决了! 这时在命令行输入:telnet localhost 1433就不会再报错了,窗口显示为一片黑,即为正常。
代码:
//=====================================================================
//
// File: connectURL.java
// Summary: This Microsoft SQL Server 2005 JDBC Driver sample application
// demonstrates how to connect to a SQL Server database by using
// a connection URL. It also demonstrates how to retrieve data
// from a SQL Server database by using an SQL statement.
// Date: April 2006
//
//---------------------------------------------------------------------
//
// This file is part of the Microsoft SQL Server JDBC Driver Code Samples.
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//=====================================================================
package mssql;
4000
import java.sql.*;
public class connectURL {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://localhost:2044;" + //主机名和加端口号,默认是1433.
"user=nono;password=nono123"; //用户名和密码。。。应该不用说了吧
// 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 * from test";
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(1));
//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) {}
}
}
}
相关文章推荐
- 【已解决】Eclipse的java代码出错:The import org.apache cannot be resolved
- 解决myeclipse中运行java代码,无法连接hadoop的问题
- 解决XDoclet解析Java 5泛型代码出错
- Navicat for MySQL连接MYSQL出错,错误代码1045的解决方法
- 解决使用libhdfs.so连接hdfs出错问题java.lang.ClassNotFoundException: org.apache.hadoop.fs.F
- Navicat for MySQL连接MYSQL出错,错误代码1045的解决方法
- 修改manifest中package="application.name",在java代码中引用R出错!解决
- java网络爬虫连接超时解决实例代码
- 完整java开发中JDBC连接数据库代码和步骤
- java后端解决跨域问题 新建一个filter 复制代码 public class SimpleCORSFilter implements Filter{ @Override
- java代码连接mysql工具类
- Java连接mysql中遇到的一些问题及解决方法
- Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决
- Eclipse 连接 SQL Server 2008数据库,运行JAVA代码时出现:‘sa’用户登录失败
- 原Tracker Benchmark v1.0 代码出错解决方法
- Java连接MySQL因字符编码不统一导致数据无法正常响应交互解决方法
- IPC连接出错的解决方法
- 经典田忌赛马算法JAVA解决代码
- 使用Win7连接虚拟机linux中的telnet出错解决方法