Comments

Friday, July 1, 2011

Microsoft SQL Server 2008 connection using ODBC tutorial

Posted by on Friday, July 1, 2011 Read our previous post
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;
      try{
         
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         con=DriverManager.getConnection("jdbc:odbc:YourDataSource");
         System.out.println("Connected");
         }catch(Exception e){
          System.out.println(e.getMessage());
         }
}
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.


© 2010 Code 2 Learn