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{
        Connection con = null;    
        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);


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

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

    }// end try

        catch(ClassNotFoundException | SQLException 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?

Recommended Answers

All 5 Replies

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, learning, and sharing knowledge.