Getting ClassNotFoundException on code: “Class.forName(”com.microsoft.sqlserver.jdbc.SqlServerDriver“);”

↘锁芯ラ 提交于 2019-12-04 12:04:26

According to this page, the class is called SQLServerDriver and not SqlServerDriver. Case is important!

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.

Java: JDBC Connectivity with MSSQL in NetBeans

Steps

  1. Download JDBC from: https://www.microsoft.com/en-in/download/details.aspx?id=11774
  2. Run sqljdbc__enu.exe - unpack this zip file in %Program Files (x86)% with the default directory: Microsoft JDBC DRIVER for SQL Server
  3. Create your new project in NetBeans
  4. Right Click on the project - select Properties - select Libraries from left panel - click Add JAR/Folder button - select your JAR file and open - ok
  5. 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
  6. Open Run and type Services.msc - Start SQL Server Browser
  7. Goto project and write code for database connectivity.

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!