ConnectSQL.java编译能通过,但在解释的时候提示:SQLException:
Message:到主机  的 TCP/IP 连接失败。 java.net.ConnectException: Connection refus
ed: connect
程序如下:import java.sql.*;
import java.net.URL;
public class ConnectSQL{
  public static void main(String[] args){
  String url="jdbc:sqlserver://localhost;DatabaseName=experiment;user=sa;password=;";
     String sql="insert into student(name,sex,age,address) values('tom','男',20,'河南')";
     try{           
      try{
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          }
   catch(java.lang.ClassNotFoundException e){
     System.out.println("Can not load Jdbc driver");
     System.out.println("ClassNotFoundException:");
     System.out.println(e.getMessage());   
   }
   Connection con=DriverManager.getConnection(url);
   Statement stmt=con.createStatement();
   stmt.executeUpdate(sql);
         }
     catch(SQLException ex){
         System.out.println("SQLException:");
         while(ex!=null){
             System.out.println("Message:"+ex.getMessage());
             ex=ex.getNextException();
          }
      }
   }
}
请高手赐教

解决方案 »

  1.   

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");写反了,
    应该是Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
      

  2.   

    楼上不对,我用的是Microsoft SQL Server 2005 JDBC Driver,如果是上述错误,编译就通不过,更别提解释运行了。请高人再赐教。谢谢
      

  3.   

    ://localhost
    这里应该指名端口号1433吧,lz试试看
      

  4.   

    您有可能是没有安装sql2000的sp3或sp4补丁
      

  5.   

    必须要有端口号默认端口是:1433
    如String url="jdbc:sqlserver://localhost:1433;DatabaseName=experiment;user=sa;password=;";
    还有就是password=""分号应该不需要了吧最好这样写容易看:
    String url="jdbc:sqlserver://localhost:1433;DatabaseName=experiment"
    String user="sa";
    String password="";
    然后在 Connection con=DriverManager.getConnection(url,user,password);
    试试吧
      

  6.   

    lz先看看telnet localhost 1433 是否能够连通
      

  7.   

    应该是端口没开放。
    sqlserver没驱动