2 u012361118 u012361118 于 2013.11.04 15:26 提问

为什么在MyEclips下运行链接数据库的java程序会报异常,但在dos下可以。

import java.sql.*;

public class Connect {

/**
 * @param args
 */ 
public void getConnection()  
 {  

     String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";  
     Connection dbConn=null;  
     try{  
          Class.forName(driverName);  
       System.out.println("ok");  
       Connection  con = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=master;user=huyaozhong;password=123456");  
       System.out.println("Connection Successful!");           
     } catch (Exception e){  
         e.printStackTrace();  
     }  
 }  

public static void main(String [] args)  
{  
Connect conn=new Connect();  
 conn.getConnection();  
 } 

}

MyEclipse下的运行结果:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Connect.getConnection(Connect.java:13)
at Connect.main(Connect.java:25)
dos下运行结果:
ok
Connection Successful!

Csdn user default icon
上传中...
上传图片
插入图片