转载自: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
下面是一个例子,已经封装到函数:
- 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());
- }
- }