我想使用Java连接到SQL Server,请找到下面的代码,我得到如下“错误消息”中给出的异常

时间:2012-11-26 08:15:21

标签: java sql-server-2008

String dburl="jdbc:sqlserver://209.18.103.198:1433;CreditDB_V2";
  //Connecting to Database
 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
 Connection con=java.sql.DriverManager.getConnection(dburl,"700User","cred!tUser");
 Thread.sleep(20000);
 if (con!=null)
   {
    System.out.println("Database Connected Successfully");
    Statement stmt=con.createStatement();
    ResultSet rss=stmt.executeQuery("update User_LogDetail set LoginTime=(GETDATE()-   
        35), StatusId = 401 where UserId in(select userid from user_detail where UserName 
        in('sahooxml','gopal','psahoo7'))");
 Thread.sleep(30000);
  while (rss.next()) {
    Object dbtime = rss.getString(1);
    System.out.println(dbtime);
   } //end while
 con.close();
 }
 else
  {
    System.out.println("Database Failed to connect");
  }

错误讯息:

Exception in thread "main" 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)
    at Login700.LoginTest(Login700.java:26)
    at Login700.main(Login700.java:242)

1 个答案:

答案 0 :(得分:3)

包含“com.microsoft.sqlserver.jdbc.SQLServerDriver”类的jar文件必须位于类路径中。