当前位置: 代码迷 >> Java相关 >> 我的SQL数据库为什么连接不上呢?
  详细解决方案

我的SQL数据库为什么连接不上呢?

热度:93   发布时间:2008-06-04 23:17:03.0
我的SQL数据库为什么连接不上呢?
import java.sql.*;


public class JDBCExample{
    public static void main(String args[]){
        try{           
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
            Connection conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=master","sa","");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from student");
            while(rs.next()){
                System.out.print("学号: " + rs.getInt(1));
                System.out.print("\t姓名: " + rs.getString(2));
                System.out.print("\t性别: " + rs.getInt(3));    
                System.out.println("\t年龄: " + rs.getString(4));
            }
            rs.close();
            stmt.close();
            conn.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}


下面的是异常信息:
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:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at test.JDBCExample.main(JDBCExample.java:9)
搜索更多相关的解决方案: SQL  数据库  public  java  stmt  

----------------解决方案--------------------------------------------------------
SQL驱动程序没有打到JAVA包下面
*:\*\Java\jdk1.6.0_03\jre\lib\ext
将SQL下的JAVA包复制到这个位置就行了!
----------------解决方案--------------------------------------------------------
补充一点 sql server数据库还需要安装sp4补丁
----------------解决方案--------------------------------------------------------
好像是SP3补丁吧!
----------------解决方案--------------------------------------------------------
[bo][un]elan1986[/un] 在 2008-6-7 11:23 的发言:[/bo]

好像是SP3补丁吧!

SQL2000-KB884525-SP4-x86-CHS.EXE这是下载的文件名 你说SP4还是SP3
----------------解决方案--------------------------------------------------------
  相关解决方案