Hello! I have written a simple program which connects netbeans with

SQL Server database. The program is giving an exception. I am

displaying the source code of my program and exception below. Please

guide me to solve this problem.

public class dbconnection {
    Connection con = null;

    public static void main(String args[]) throws ClassNotFoundException, SQLException{
        try
        {
        Connection con = null;    
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection("jdbc:sqlserver://SABOOR-PC\\SQLEXPRESS;databaseName = Test;integratedSecurity = true;");
        Statement st = con.createStatement();

        String sql = "SELECT *FROM Employees";

        ResultSet rs = st.executeQuery(sql);

        while(rs.next())
        {

            String EmployeeName = rs.getString("EmpName");
            String EmployeeWage = rs.getString("Wage");

        System.out.println(EmployeeName + " "+ EmployeeWage);
          }// end while
        con.close();

    }// end try

        catch(ClassNotFoundException | SQLException sqlEx)
        {
           System.out.println(sqlEx);
        }
 }
}

Now I am also giving exception which is occuring:
"java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver"

As you can see above exception please tell me that why this exception is occuring and how can I handle this exception?

Dani AI

Generated

Quick expert note for (and anyone hitting the same error)

The exception means the JVM could not load the Microsoft JDBC driver class at runtime. That almost always comes down to the driver not being on the runtime classpath, the wrong driver JAR/version, or an accidental typo. Adding the JAR to your NetBeans project compile path is not enough unless it is also on the run classpath (or bundled with the app at runtime).

Checklist to fix and verify

  • Make sure the Microsoft JDBC JAR is on the runtime classpath. In NetBeans add the JAR via Project > Properties > Libraries (or add a Maven dependency). Then Clean and Build and rerun.
  • Verify the JAR actually contains the driver class by listing its contents (example):
    jar tf path\to\driver.jar | findstr SQLServerDriver
  • If you use integratedSecurity=true, place the matching sqljdbc_auth.dll (32-bit vs 64-bit must match the JVM) on the system PATH or pass -Djava.library.path=C:\path\to\dll. Without that DLL you will get authentication/native library errors even after the driver loads.
  • Remove stray spaces in the connection URL key/value pairs (use databaseName=Test;integratedSecurity=true;) and ensure Java string backslashes are escaped when specifying an instance name.

Notes on driver loading and best practice

  • As noted, current Microsoft drivers auto-register with the DriverManager, so you normally do not need Class.forName(...). Explicitly calling DriverManager.registerDriver(...) can work for quick tests (and solved the immediate issue here), but the long-term fix is correct placement of the JAR so the driver registers automatically.
  • As suggested, prefer selecting explicit columns instead of SELECT *. Also use try-with-resources and PreparedStatement to manage resources and avoid leaks. Example pattern:
    try (Connection c = DriverManager.getConnection(url);
       PreparedStatement ps = c.prepareStatement("SELECT EmpName, Wage FROM Employees");
       ResultSet rs = ps.executeQuery()) {
      while (rs.next()) { /* use rs.getString(...) */ }
    }

    Follow the checklist above and the driver will load reliably at runtime.

Recommended Answers

All 5 Replies

You are using a seriously out-of-date source for your info.
The whole Class.forName thing was replaced as of Java 1.4 or 1.5 (Yes, that's more than 10 years ago).
Forget the obsolete info you have been given and start again with current Java. Here's the up-to-date tutorial:
http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

Thanx James. But I am still stuck on the same point. I viewed the link you gave, there is also "Class.forName" is used as well as "DriverManager.Register" is also used. But in my code the problem is not due to "Class.forName" key word but the problem is due to ther driver class which i haved loaded i-e "com.microsoft.sqlserver.jdbc.SQLServerDriver". Even I have added sqljdb4.jar file in my project's class path but I am still facing this exception. I hope that you understood my problem. So please try to solve my problem, I will be thankfull to you.

"There is also Class.forName is used" - is that how you interpret
"In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName." ?
It's obsolete!

There is a probem loading the class, so the first thing to do is to make sure you are not using some obsolete method to load it.

ps Class.forNameis a method, not a keyword. That's a very important difference.

Thank you very much JamesCherrill. My problem has solved by using DriverManager.RegisterDriver Method. Thanx again bro.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.