您的位置:首页 > 编程语言 > Java开发

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

2013-12-26 22:50 771 查看
转载自:http://blog.csdn.net/autofei/article/details/4064299

SQL Server 2000 JDBC driver使用:"com.microsoft.jdbc.sqlserver.SQLServerDriver"

SQL Server 2005 and 2008 JDBC drive使用:"com.microsoft.sqlserver.jdbc.SQLServerDriver"

而且URL prefix也从"jdbc:microsoft:sqlserver://"改为"jdbc:sqlserver://"了。

 

From:http://blogs.msdn.com/jdbcteam/archive/2007/06/15/java-lang-classnotfoundexception-com-microsoft-jdbc-sqlserver-sqlserverdriver.aspx

 

下面是一个例子,已经封装到函数:

[java] view
plaincopy

 public static Connection linktoSQLServer() {  

    //String connect to SQL server  

    String strServerIPAddress = "localhost";  

    String strDatabaseName = "AdventureWorks";  

    String url = "jdbc:sqlserver://" + strServerIPAddress + ":1433" +  

            ";DatabaseName=" + strDatabaseName;  

    String strUserName = "sa";  

    String strPassword = "123456";  

    //Connection conn = null;  

    try {  

        //  Register the driver  

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();  

        try {  

            /* Get a connection to the database */  

            conn = DriverManager.getConnection(url, strUserName, strPassword);  

        } catch (SQLException ex) {  

            System.out.println("Exception One:");  

            ex.printStackTrace();  

        }  

    } catch (Exception ex) {  

        System.out.println("Exception Two:");  

        ex.printStackTrace();  

    }  

    System.out.println("Connected...");  

    return conn;  

}  

public static void SQLServer_select() {  

    //this code just for SQL Server  

    try {  

        String strCmd = "SELECT * FROM Production.Location";  

        rs = stmt.executeQuery(strCmd);  

        while (rs.next()) {  

            System.out.println("Column 1 has value: " + rs.getString(1));  

            System.out.println("Column 2 has value: " + rs.getString(2));  

            System.out.println("Column 3 has value: " + rs.getString(3));  

        }  

    } catch (SQLException ex) {  

        System.out.println("SQLException: " + ex.getMessage());  

        System.out.println("SQLState: " + ex.getSQLState());  

        System.out.println("VendorError: " + ex.getErrorCode());  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql java
相关文章推荐