public class DatabaseOperation {
Connection conn= null;
/**
*
* 取得当前数据库的连接
*
* @param connHostName 主机名称或IP Address
* @param connPort 连接数据库端口
* @param dbDBAUserName 连接数据库DBA用户名
* @param dbDBAPassword 连接数据库DBA用户密码
* @return
*/
public Connection connectDatabase(String connHostName,String connPort,String dbDBAUserName,String dbDBAPassword) {
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String url = "jdbc:sqlserver://" + connHostName + ":" + connPort + ";DatabaseName=master";
conn = DriverManager.getConnection(url, dbDBAUserName, dbDBAPassword);
setConn(conn);
}catch (Exception ex){
ex.printStackTrace();
setConn(null);
}
return getConn();
}
main方法
public static void main(String[] args) {
DatabaseOperation db = new DatabaseOperation();
System.out.println("============"+ db.connectDatabase("localhost", "1433", "sa", "123456"));;
}
端口是1433
驱动的包没有错,因为可以连接别人的数据库、
报错如下:
[color=red]com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at org.com.sap.db.DatabaseOperation.connectDatabase(DatabaseOperation.java:55)
at org.com.sap.db.DatabaseOperation.main(DatabaseOperation.java:229)
============null[/color]
没有高人知道、、指点一二
“Connection refused: connect
com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
java 连sql2005出现这个错误时,首先检查是否引用了sql 2005的包;
第二步,看你的sql 2005是否开启了远程访问权限,具体设置如下:
进入“Microsoft SQL Server 2005 ”——“配置工具”——“SQL Server 外围应用配置器”
在进入“MSSQLSERVER"——“DATABASE" Engine”——“远程连接”
选择“本地连接和远程连接(R)"——”同时使用tcp/ip和named pipes(B)"
最后重启sql server 2005 ”
试过了,不是这个问题。。。
[b]问题补充:[/b]
打开了、、、、
但是网络服务器那个,端口号(TCP Port),我不知道怎么填写。
[b]问题补充:[/b]
[url]http://dl.iteye.com/upload/attachment/183234/7a07e7f1-8af7-30e5-a303-104a9677d6cb-thumb.jpg[/url]
[b]问题补充:[/b]
SQL server Configuration Manager
在网络配置中已经是启用TCP/IP了
感觉是当做服务器的时候,端口号没有填、、、
[b]问题补充:[/b]
看见端口都改成1433了。
网络那边是1433
客户端也是1433
[b]问题补充:[/b]
C:\Documents and Settings\C5136015>n
Active Connections
Proto Local Address Fore
TCP 0.0.0.0:135 0.0.
TCP 0.0.0.0:445 0.0.
TCP 0.0.0.0:2180 0.0.
TCP 0.0.0.0:2738 0.0.
TCP 0.0.0.0:18102 0.0.
TCP 10.58.71.159:139 0.0.
TCP 10.58.71.159:1244 10.2
TCP 10.58.71.159:1336 10.2
TCP 10.58.71.159:1337 10.2
TCP 10.58.71.159:1341 10.1
TCP 10.58.71.159:1357 10.5
TCP 10.58.71.159:1437 10.5
TCP 10.58.71.159:1711 10.5
TCP 10.58.71.159:1712 10.5
TCP 10.58.71.159:1890 10.5
TCP 10.58.71.159:1894 10.5
TCP 10.58.71.159:1895 10.5
TCP 10.58.71.159:1896 10.5
TCP 10.58.71.159:1897 10.5
TCP 10.58.71.159:14599 0.0.
TCP 127.0.0.1:5152 0.0.
UDP 0.0.0.0:445 :
UDP 0.0.0.0:500 :
UDP 0.0.0.0:1240 :
UDP 0.0.0.0:4500 :
UDP 0.0.0.0:6004 :
UDP 0.0.0.0:18102 :
UDP 0.0.0.0:18103 :
UDP 10.58.71.159:123 :
UDP 10.58.71.159:137 :
UDP 10.58.71.159:138 :
UDP 127.0.0.1:123 :
UDP 127.0.0.1:1025 :
UDP 127.0.0.1:1068 :
UDP 127.0.0.1:1126 :
UDP 127.0.0.1:1315 :
UDP 127.0.0.1:1328 :
UDP 127.0.0.1:1342 :
UDP 127.0.0.1:1447 :
UDP 127.0.0.1:1562 :
UDP 127.0.0.1:1666 :
UDP 127.0.0.1:1698 :
UDP 127.0.0.1:1750 :
UDP 127.0.0.1:10000 :
[b]问题补充:[/b]
1433的端口没有打开,我需要手动打开?还是这个端口已经被占用了
[b]问题补充:[/b]
那怎么打开啊?我刚才添加了1433的端口,要重启么?
[b]问题补充:[/b]
重启了还是没有1433端口,按照别人的方法添加了。。。。