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?

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.