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
298 views
in Technique[技术] by (71.8m points)

java - classNotFoundException in Loading JDBC Driver

I am a newbie in java and I'm developing a Java EE application on the Netbeans 6.9.1 IDE. I have to connect my java application with SQL Server 2005.

For that I have downloaded the sqljdbc.jar file and have put it into C:Program Files (x86)Microsoft SQL ServerJDBC Drverlib on my system and have set its classpath on command prompt like this

set classpath=.;C:Program Files (x86)Microsoft SQL ServerJDBC Drverlibsqljdbc.jar

and have set the classpath in the IDE by right clicking on the main project and selecting its property selecting libraries. Then in compile tab added a sqljdbc.jar, but when I execute this code

import java.sql.*;
/**
 *
 * @author abc
 */
public class DBConnection
{
public Connection dbConnect(String db_connect_string)
        {
                try
                {

                        Class.forName(
                          "com.microsoft.jdbc.sqlserver.SQLServerDriver");

                        Connection conn =
                          DriverManager.getConnection(db_connect_string);

                        System.out.println("connected");
                        return conn;

                }
                catch (Exception e)
                {
                        System.out.println(e);
                    e.printStackTrace();
                        return null;
                }
        }
}

it is giving me ClassNotFound error on this line Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You need to check the JDBC driver documentation which came along with your SQL server version. In the old SQL Server 2000, the JDBC driver class name is like as you have:

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

However, since SQL Server 2005, Microsoft changed the JDBC driver class name:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Fix it accordingly.

Please note that the CLASSPATH environment variable is ignored by Netbeans and all other decent Java programs. Forget about it and don't even try to set it until you understand why it exists and what it is to be used for.


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

...