Java SQl Server连接错误

时间:2011-11-11 09:33:01

标签: java sql-server sql-server-2005 jdbc

我已经编写了sql server连接部分。当我运行此代码时,我收到了此错误。

    Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver
Error: No active Connection
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.sample.DB.getConnection(DB.java:30)
    at com.sample.DB.displayDbProperties(DB.java:49)
    at com.sample.DB.main(DB.java:85)

我已经安装了sql sever 2008 R2。我serached谷歌我找不到解决方案....

这是我的代码

    public class DB {
    private java.sql.Connection  con = null;
    private final String url = "jdbc:microsoft:sqlserver://";
    private final String serverName= "localhost";
    private final String portNumber = "1433";
    private final String databaseName= "XONTHOMass_User";
    private final String userName = "sa";
    private final String password = "xont@123";
    // Informs the driver to use server a side-cursor, 
    // which permits more than one active statement 
    // on a connection.
    private final String selectMethod = "cursor"; 



    private String getConnectionUrl(){
         return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
    }

    private java.sql.Connection getConnection(){
         try{
             System.out.println("========1========");
             Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
             System.out.println("==== 2=====");
             con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
             if(con!=null) System.out.println("Connection Successful!");
         }catch(Exception e){
              e.printStackTrace();
              System.out.println("Error Trace in getConnection() : " + e.getMessage());
        }
         return con;
     }

    /*
         Display the driver properties, database details 
    */ 

    public void displayDbProperties(){
         java.sql.DatabaseMetaData dm = null;
         java.sql.ResultSet rs = null;
         try{
              con= this.getConnection();
              if(con!=null){
                   dm = con.getMetaData();
                   System.out.println("Driver Information");
                   System.out.println("\tDriver Name: "+ dm.getDriverName());
                   System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                   System.out.println("\nDatabase Information ");
                   System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                   System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                   System.out.println("Avalilable Catalogs ");
                   rs = dm.getCatalogs();
                   while(rs.next()){
                        System.out.println("\tcatalog: "+ rs.getString(1));
                   } 
                   rs.close();
                   rs = null;
                   closeConnection();
              }else System.out.println("Error: No active Connection");
         }catch(Exception e){
              e.printStackTrace();
         }
         dm=null;
    }     

    private void closeConnection(){
         try{
              if(con!=null)
                   con.close();
              con=null;
         }catch(Exception e){
              e.printStackTrace();
         }
    }
    public static void main(String[] args) throws Exception
      {
         DB myDbTest = new DB();
         myDbTest.displayDbProperties();
      }
}

请帮助我..我使用eclipse做了这个应用程序。我把jar文件也放在`sqljdbc4.jar'...

请帮帮我

4 个答案:

答案 0 :(得分:1)

您收到例外:

  

java.lang.ClassNotFoundException:com.microsoft.jdbc.sqlserver.SQLServerDriver

这意味着您的程序无法找到驱动程序,因此甚至无法连接到数据库。

Mircosoft有一篇关于How to Get Started with Microsoft JDBC的文章:

  

必须在CLASSPATH变量中列出JDBC .jar文件的Microsoft SQL Server 2000驱动程序。 CLASSPATH变量是Java虚拟机(JVM)用于在计算机上查找JDBC驱动程序的搜索字符串。

答案 1 :(得分:0)

完整的猜测是,您已经安装了Express Edition(您应该始终提及版本,而不仅仅是版本),但您尚未使用SQL Server配置管理器启用TCP / IP支持。默认情况下,Express Edition没有启用任何网络协议,因此除非您先启用它,否则无法通过TCP / IP进行连接。

答案 2 :(得分:0)

我认为这是您需要使用的驱动程序

com.microsoft.sqlserver.jdbc.SQLServerDriver

不是com.microsoft.jdbc.sqlserver.SQLServerDriver

答案 3 :(得分:0)

只需做一件事就转到http://www.java2s.com/Code/Jar/s/Downloadsqljdbcjar.htm并下载sqljdbc jar并在你的程序中使用它。