我想用JDBC連接到SQL Server 2008。我的SQL Server 2008擁有Windows身份驗證。使用jdbc連接到sql server
我的代碼是
public class T1 {
/**
* @param args
*/
private static void Connect(){
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://servername;databaseName=employee;user=username;password=''/*since it is windows authentication*/;";
java.sql.Connection con = DriverManager.getConnection(connectionUrl);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e2)
{
e2.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
T1.Connect();
}
我最終得到一個ClassNotFoundException。
堆棧跟蹤爲java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.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.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source)
1)發佈的堆棧跟蹤2 )是類路徑中的連接器jar? –
您是否閱讀過Microsoft文檔? –
[可能重複](http://stackoverflow.com/questions/6372815/class-notfound-exception-in-sqlserver-connection-in-eclipse)如果您使用的是命令提示符,請參考此處的classpath用法[參考]( http://stackoverflow.com/questions/2526048/classnotfoundexception-when-connecting-to-mysql-with-jdbc) – gks