Java 在代码上获取 ClassNotFoundException:"Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18420675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting ClassNotFoundException on code: "Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");"
提问by user2708377
This is my first Javaapplication and I'm completely inexperienced with Javaand NetBeans.
这是我的第一个Java应用程序,我对Java和NetBeans完全没有经验。
I have been trying to connect to sql and get some records for 2 days. The problem is about jdbcdriver, let me explain. I have downloaded sqljdbcdriver and then followed these steps:
我一直在尝试连接到 sql 并获取一些记录 2 天。问题是关于jdbc驱动程序,让我解释一下。我已经下载了sqljdbc驱动程序,然后按照以下步骤操作:
Right-click Project->Select Properties->On the left-hand side click Libraries->Under Compile tab - click Add Jar/Folder button and select sqljdbc4.jarfile. Then it should be ok, right?
右键单击项目-> 选择属性-> 在左侧单击库-> 在编译选项卡下 - 单击添加 Jar/文件夹按钮并选择sqljdbc4.jar文件。那应该没问题吧?
Then I wrote this code But I cant get rid of this exception:
然后我写了这段代码但是我无法摆脱这个异常:
Exception in thread "main" java.lang.ClassNotFoundException:
com.microsoft.sqlserver.jdbc.SqlServerDriver
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:30)
This is the code
这是代码
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url = "jdbc:sqlserver://.\SQLEXPRESS;databaseName=Northwind; Integrated Security = SSPI ";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
con = DriverManager.getConnection(url);
String sql = "Select Top 3 from * person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
采纳答案by Jesper
According to this page, the class is called SQLServerDriver
and not SqlServerDriver
. Case is important!
根据此页面,该类被调用SQLServerDriver
而不是SqlServerDriver
。案例很重要!
So, try:
所以,试试:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Note that with newer versions of JDBC, it's not necessary to load the driver class explicitly with Class.forName(...)
. The page I linked to explicitly explains that you don't have to do it. So, you can just remove the whole line and then it should work.
请注意,对于较新版本的 JDBC,没有必要使用Class.forName(...)
. 我链接到的页面明确说明您不必这样做。因此,您可以删除整行,然后它应该可以工作。
回答by sanjib kumar Panigrahi
Java: JDBC Connectivity with MSSQL in NetBeans
Java:在 NetBeans 中使用 MSSQL 的 JDBC 连接
Steps
脚步
- Download JDBC from: https://www.microsoft.com/en-in/download/details.aspx?id=11774
- Run sqljdbc__enu.exe - unpack this zip file in %Program Files (x86)% with the default directory: Microsoft JDBC DRIVER for SQL Server
- Create your new project in NetBeans
- Right Click on the project - select Properties - select Libraries from left panel - click Add JAR/Folder button - select your JAR file and open - ok
- Open Sql Server Configuration Manager - select Protocols for SQLEXPRESS under Sql Server Network Configuration - Right Click on TCP/IP - select Properties - change Enable to Yes - Click IP Addresses - Goto IPAll - Change TCP Dynamic Ports to 49169 and TCP Port to 1433 - Apply - Ok - Restart the Computer
- Open Run and type Services.msc - Start SQL Server Browser
- Goto project and write code for database connectivity.
- 从以下位置下载 JDBC:https: //www.microsoft.com/en-in/download/details.aspx?id=11774
- 运行 sqljdbc__enu.exe - 将此 zip 文件解压到 %Program Files (x86)% 中,默认目录为:Microsoft JDBC DRIVER for SQL Server
- 在 NetBeans 中创建新项目
- 右键单击项目 - 选择属性 - 从左侧面板中选择库 - 单击添加 JAR/文件夹按钮 - 选择您的 JAR 文件并打开 - 确定
- 打开Sql Server配置管理器-在Sql Server网络配置下选择SQLEXPRESS的协议-右键单击TCP/IP-选择属性-将启用更改为是-单击IP地址-转到IPAll-将TCP动态端口更改为49169,将TCP端口更改为1433-应用 - 确定 - 重新启动计算机
- 打开运行并键入 Services.msc - 启动 SQL Server 浏览器
- 转到项目并编写用于数据库连接的代码。
Code for Local Database Connectivity:
本地数据库连接代码:
String url = "jdbc:sqlserver://YOUR PC NAME;instanceName=SQLEXPRESS;DatabaseName=dbname;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection myCon = DriverManager.getConnection(url);
Statement myStmt = myCon.createStatement();
ResultSet myRs = myStmt.executeQuery("select * from table name");