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

wzb56 2008-12-05 06:16:49
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)
请指点。
...全文
65 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cy729215495 2008-12-09
  • 打赏
  • 举报
回复
还有个问题也要注意,"com.microsoft.jdbc.sqlserver.SQLServerDriver"它需要三个jar包,很麻烦的,建议直接用jtds的驱动
xql80329 2008-12-09
  • 打赏
  • 举报
回复
sql2000 装好首先要打补丁
wzb56 2008-12-09
  • 打赏
  • 举报
回复
谢谢大侠们的指点,原来是我没有装sp4的补丁!!!!!!!!!!!以后一定注意格式的,呵呵,再谢谢二楼喽!!!!!!!!
wzb56 2008-12-09
  • 打赏
  • 举报
回复
谢谢大侠们的指点,原来是我没有装sp4的补丁!!!!!!!!!!!
wzb56 2008-12-09
  • 打赏
  • 举报
回复
谢谢大侠们的指点,原来是我没有装sp4的补丁!!!!!!!!!!!
ar7043 2008-12-09
  • 打赏
  • 举报
回复
打补丁
SINCE1978 2008-12-05
  • 打赏
  • 举报
回复
0、检查sqlserver的登陆方式是否为混合式并尝试用查询管理器按sqlserver认证能否登陆
1、关掉防火墙再试一下
lshy168 2008-12-05
  • 打赏
  • 举报
回复

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!!");
// 你这步出现了异常,但是你写的没有错,看异常报的是建立socket错误,你有没有打上SQLServer 2000
// 的sp4的补丁?或者是你端口号是否正确?
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();
}
}

}
}
lshy168 2008-12-05
  • 打赏
  • 举报
回复
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!!");

//你这步出现了异常,但是你写的没有错,看异常报的是建立socket错误,你有没有打上SQLServer 2000 的sp4的补丁?或者是你端口号是否正确?
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();
}
}

}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧