Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

sql server - java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver : Am I loading the right driver?

When I run the following snippet to connect to datasource to get the data out through queries :

        try {
        String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
        Class.forName(driver);
        String Url = "jdbc:sqlserver://localhost:1433;databaseName=movies";
        Connection connection = DriverManager.getConnection(Url,"sa", "xxxxxxx);
        //            Context initContext = new InitialContext();
        //            Context envContext = (Context)initContext.lookup("java:comp/env");
        //            DataSource ds = (DataSource)envContext.lookup("");
        //            Connection connection = ds.getConnection();
        String query_name = "select name from list where name='" + data + "'";

this exception trace is shown :

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at gui.MainGui.startSqlServerDataSearch(MainGui.java:148)
at gui.MainGui$3.run(MainGui.java:129)
at java.lang.Thread.run(Thread.java:619)

Why am I getting this exception ? I have already made a connection to the sql server (by loading the drivers as given here) and can see the database defined in sql server. The following snapshot tells this :

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Having database defined and Class.forName(driver); is not enough.

You need to make sure appropriate database driver jar is in classpath.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...