Microsoft SQL Server 2008 connection using ODBC tutorial

My previous post was on SQL Server installation . Now today I will be telling how to connect your database ie SQL Server using ODBC with Netbeans.

After you installed SQL Server 2008 Express successfully, now its time to make you of it. So :

1. Go START -> Programs -> Microsoft SQL Server 2008 -> Configuration Tools -> SQL Server Configuration Manager.

2. When you on it, you will get a window like below :

Above in the yellow color zone, you will see the instance name that you gave during the installation. So right click on that and press START. Now your server will start. If already start then leave it.

3. Now again goto START -> Programs -> Microsoft SQL Server 2008 -> SQL Server Management Studio.4. As you click it a window will open like this :

In the above picture you can see that the Highlighted thing. that is the instance that made of the server during installation.

If nothing appear there then go Step 1 follow till step 3. And again try. You will get it.

After you click connect, the database will open that will have some default tables,views etc.
Now make a new database named "Code2Learn". and click ok.

Now your SQL SERVER is ready to work.

Configuring ODBC :

Goto Start -> and in the search bar type ODBC. right and click on "Use as Administrator".

You will get a window like below :

Now click on Add, then click and highlight SQL Server. Click finish.

Give your Data Source a name ex: "C2LData". Then the click on add server and add the server in which your SQL Server instance is there ex. "Your_PC\SQLServer", click NEXT.

Now check "Login with Windows NT authentication using the netwoek login ID." click Next.

Here  you can change the database that you want to keep as default so click "Change default Database" and choose Code2Learn (as we made this database in the Management Studio. after you do this click next and then click on FINISH. ODBC DATA SOURCE is NOW Ready.

Connection using JAVA using Netbeans IDE :

1. Open Netbeans and then add new project. Add a Java application.
2. Open the file Main.java :

first import java package ie

import java.sql.*;

and in the Main class write the code below.

public static void main(String[] args){
      Connection con=null;
         }catch(Exception e){
If you get the output as "Connected". Then the database has been 
connected and now you use it to SELECT,INSERT,UPDATE or MODIFY data.



  1. thank you.. its help me alot... :)

  2. Good tutorial Farhan, quite detailed and step by step. Just to add in case if you get java.lang.ClassNotFoundException in Java while loading sun.jdbc.odbc.JdbcOdbcDriver than check for driver's Jar. here are few ways to solve java.lang.ClassNotFoundException in Java

  3. Hi all I am using same code on JDK7 and 8. But its not working. Its giving class not found exception for sun.jdbc.odbc.JdbcOdbcDriver. How should I remove this error?

    1. You need to download JDBC driver. this link might help you:

  4. Error while lode the class is java.langg.ClassNotFoundException: sun. Jdbc.odbc.JdbcOdbcDriver please give me the solution

    sqlserver 2008 R2
    Jdk 1.8