2011-11-11 15 views
0

私はこのコードを実行すると、私はこのエラーが発生しました。Java SQlサーバーの接続エラー

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 server 2008 R2をインストールしました。私は

は、これは私のコード

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 = "[email protected]"; 
    // 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(); 
     } 
} 

me..Iがeclipse.Iを使用して、このアプリケーションはまた、jarファイルを入れて持っていた助けてください `sqljdbc4です....私は解決策を見つけることができませんでしたグーグルserached ...」の.jar

が完了推測として私に

答えて

1

あなたが例外受け取る:

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

あなたのプログラムがドライバを見つけられないので、さえしようとしないことを意味しデータベースに接続します。

MircosoftはHow to Get Started with Microsoft JDBCの記事があります。

JDBC .jarファイルは、Microsoft SQL Server 2000のドライバは、あなたのCLASSPATH変数にリストされている必要があります。 CLASSPATH変数は、私はこれがあなたが

com.microsoft.sqlserver.jdbc.SQLServerDriver 

をいない使用する必要があるドライバーだと思います。.. Java仮想マシン(JVM)は、コンピュータ上のJDBCドライバを見つけるために使用する

0

を助けてください、あなたは(あなたがいつも版だけでなく、バ​​ージョンを言及する必要があります)Express Editionをインストールしましたが、あなたは使用してTCP/IPのサポートを有効にしていませんSQL Server構成マネージャー。デフォルトでは、Express Editionにはネットワークプロトコルが有効になっていないため、TCP/IP経由での接続は、最初に有効にしないと機能しません。

0

検索文字列でありますcom.microsoft.jdbc.sqlserver.SQLServerDriver

関連する問題