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()); } }
本文介绍了如何使用 JDBC 连接到 SQL Server 数据库,包括不同版本 SQL Server 的驱动名称变化及 URL 前缀更新,并提供了一个 Java 代码示例来演示连接过程及简单查询。
1554

被折叠的 条评论
为什么被折叠?



