加载JDBC驱动程序中的classNotFoundException [英] classNotFoundException in Loading JDBC Driver

查看:847
本文介绍了加载JDBC驱动程序中的classNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java的新手,我正在Netbeans 6.9.1 IDE上开发Java EE应用程序。我必须将我的Java应用程序与SQL Server 2005连接。

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.

为此我已经下载了sqljdbc.jar文件并将其放入 C:我的系统上有\Program Files(x86)\ Microsoft SQL Server \ _JDBC Drver \ lib ,并在命令提示符下设置了类路径

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

set classpath=.;C:\Program Files (x86)\Microsoft SQL Server\JDBC Drver\lib\sqljdbc.jar

并通过右键单击主项目并选择其属性选择库,在IDE中设置类路径。然后在编译选项卡中添加了 sqljdbc.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;
                }
        }
}

它给了我ClassNotFound错误在这一行 Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver);

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

推荐答案

您需要检查随SQL Server版本提供的JDBC驱动程序文档。在旧的 SQL Server 2000 中,JDBC驱动程序类名称与您一样:

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");

但是,因为 SQL Server 2005 ,Microsoft更改了JDBC驱动程序类名称:

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

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

相应修正。

请注意Netbeans和所有其他不错的Java程序忽略 CLASSPATH 环境变量。忘记它,甚至不要尝试设置它,直到你理解为什么它存在以及它用于什么。

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.

这篇关于加载JDBC驱动程序中的classNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆