sqlserver2005 安装包下载地址:http://pan.baidu.com/s/1bnq4msv
安装截图:
(1)使用SQL Server Management Studio 导入mdf文件
(2)SQL Server Management Studio 连接sqlserver2005
连接地址应该是:WIN7-20070102UA\SQLSERVER2005
\ 左边是计算机名称或者ip,\ 右边是sqlserver2005 的实例名称
注意:不是服务名,而是实例名,以我的sqlserver2005举例,实例名是SQLSERVER2005,服务名是:MSSQL$SQLSERVER2005
(3)Java 连接sqlserver2005
报错com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败
- 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:571)
- at java.sql.DriverManager.getConnection(DriverManager.java:215)
- at com.hw.util.DbUtil.getConn(DbUtil.java:23)
- at com.hw.util.DbUtil.main(DbUtil.java:33)
原因:sqlserver2005 的TCP/IP没有开启,按下图所示开启即可.
注意:sqlserver2005 的jdbc 端口号不是1433,而是58076
java 通过jdbc连接sqlserver2005 代码 :
- public class DbUtil {
- private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- private String url ="jdbc:sqlserver://localhost:58076;databasename=school";
- private String user = "sa";
- private String pwd ="1234567";
- public Connection getConn(){
- Connection conn = null;
- try {
- Class.forName(driver);
- conn = DriverManager.getConnection(url,user,pwd);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("查看数据库服务器开启");
- }
- return conn;
- }
- public static void main(String[] args) {
- DbUtil dbUtil=new DbUtil();
- System.out.println(dbUtil.getConn());
- }
时间: 2024-09-16 15:05:08