怎么正确连接sql server 2000?????

wzb56 wzb56 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
package cn.wzb;

import java.sql.*;
import java.util.Vector;

public class TestJdbcsqlServer {

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("实例化成功 is ok!!");

conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=factory","sa","sa");
System.out.println("the connection is ok!!");
Vector<String> fieldNames = new Vector<String>();
DatabaseMetaData metaData = conn.getMetaData();
rs = metaData.getColumns(null, null, "employee", null);
while(rs.next()) {
fieldNames.addElement(rs.getString(4) + "\t\t|");
}

System.out.print("Table employee:\n|");
for(int i=0;i<fieldNames.size();i++) {
System.out.print(fieldNames.elementAt(i));
}
System.out.println();

stmt = conn.createStatement();
rs = stmt.executeQuery("select * from employee order by id asc");
while(rs.next()) {
System.out.print("|");
for(int i=1;i<=fieldNames.size();i++) {
System.out.print(rs.getString(i) + "\t|");
}
System.out.println();
}

}
catch(ClassNotFoundException e) {
System.out.println("com.microsoft.jdbc.sqlserver.SQLServerDriver Is Not Found!!");
e.printStackTrace();
}
catch(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLSata: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {

if(rs != null) {
rs.close();
rs = null;
}

if(stmt != null) {
stmt.close();
stmt = null;
}

if(conn != null) {
conn.close();
conn = null;
}
}
catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e ) {
e.printStackTrace();
}
}


}

}



为什么老是有异常 ,连接不成功?我是DB新手,异常如下:

实例化成功 is ok!!
SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
SQLSata: 08001
VendorError: 0
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at cn.wzb.TestJdbcsqlServer.main(TestJdbcsqlServer.java:17)
请指点。
...全文
评论 9
收藏 已收藏
分享
返回顶部
wzb56
Java SE 62,621 成员 307,251 内容