lxm520910 发表于 2008-6-4 23:17

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


下面的是异常信息:
[color=Red]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)[/color]

elan1986 发表于 2008-6-6 09:39

SQL驱动程序没有打到JAVA包下面
*:\*\Java\jdk1.6.0_03\jre\lib\ext
将SQL下的JAVA包复制到这个位置就行了!

guoxhvip 发表于 2008-6-7 02:20

补充一点 sql server数据库还需要安装sp4补丁

elan1986 发表于 2008-6-7 11:23

好像是SP3补丁吧!

guoxhvip 发表于 2008-6-8 11:38

[quote][bo][un]elan1986[/un] 在 2008-6-7 11:23 的发言:[/bo]

好像是SP3补丁吧! [/quote]
[em10] SQL2000-KB884525-SP4-x86-CHS.EXE这是下载的文件名 你说SP4还是SP3

页: [1]

编程论坛