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)
请指点。

解决方案 »

  1.   

    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();
    }
    } }
    }
      

  2.   


    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();
    }
    } }
    }
      

  3.   

    0、检查sqlserver的登陆方式是否为混合式并尝试用查询管理器按sqlserver认证能否登陆
    1、关掉防火墙再试一下
      

  4.   

    还有个问题也要注意,"com.microsoft.jdbc.sqlserver.SQLServerDriver"它需要三个jar包,很麻烦的,建议直接用jtds的驱动