加载 JDBC 驱动程序中的 classNotFoundException [英] classNotFoundException in Loading JDBC Driver
问题描述
我是 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 ServerJDBC Drverlib
并设置了它的类路径在这样的命令提示符下
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
并通过右键单击主项目并选择其属性选择库在 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");
相应地修复它.
请注意 CLASSPATH
环境变量被 Netbeans 和所有其他体面的 Java 程序忽略.忘记它,甚至不要尝试设置它,直到您了解它的存在原因和用途.
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屋!