6/3/12

Creating a Basic JDBC application

Today we will be creating a Basic JDBC application i.e. an application that will be able to retrieve information (student_name, roll_no, class, DOB, etc) from the database. I already gave a tutorial on connecting SQL Server using JDBC-ODBC Bridge.

To do the above we will need to make use of the JDBC-ODBC Bridge driver. And also the following task will also be done :

* Create a Data Source Name (DSN)
* Code the Application
* Compile and Execute the Application

And for the first time I will be showing on Code 2 Learn how to compile and run a java program from the command promt.


Create a DSN


Now, to create a DSN we need to perform the below steps :

* Selecting the required driver for a DSN
** Specifying the connection details for a DSN
** Testing the created DSN

Selecting the Required driver for a DSN


* Click the start button and then type "odbc" and you will get the result as Data Source (ODBC), open the same as an administrator. Or if the search is not showing it, then goto Control Panel -> Administrative Tools and there you will find it.

** Now open it and you will see the dialog box as below.


* To select a driver for creating a DSN, click on ADD and then select SQL Server from the list.

Specifying the Connection Details for a DSN


After you select SQL Server as the driver for creating a DSN, and you clicked finished, a new dialog box will open asking for the Name, Description and Server.

* So give any name to the DataSource, I am giving it "Code2LearnDataSource".

* After giving DSN a name, then select the SQL Server by clicking on the drop down menu of the server. You will find the server that you made earlier using SQL Server. Take a Look at the image below, it should look like this.


* Now click on Next and it will ask, how do you want to login, you can choose what ever you want. But as far as this tutorial is concerned use the 2nd option i.e. With SQL Server..

* After you select the 2nd option you will have to set the Login ID and password, so enter admin as Login ID and leave blank in password field.

* Now when you click on NEXT you will get a conformation message saying that, Connection Successful. And then you will be asked whether you want to change the default database. If you have multiple database(s) then please make sure that you are using the right database otherwise the query will give an error.

* Click on next and then finish.

Testing the Created DSN


* After you complete the above steps, you will get a dialog box with an option to Test Data Source. So click on the button and let the JDBC-ODBC Bridge test the data source.

* If the Test is successful you will get a message saying Tests completed successfully.

* Now return to Data Source(ODBC) and check whether your data source is there or not.

Code Application 


We need to write the code for the following :

* Load JDBC-ODBC Bridge driver.
* Connect to the Code2LearnDataSource DSN.
* Create and execute the JDBC statements.
* Display results.


import java.sql.*;

public class StudentInfo
{
    public static void main(String args[])
    {
        try
        {
            String str="SELECT * FROM students";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conObj=DriverManager.getConnection("jdbc:odbc:Code2Learn"
            +"DataSource","admin","");
            Statement stmtObj=conObj.createStatement();
            ResultSet rsObj=stmtObj.executeQuery(str);
            while(rsObj.next())
            {
                String name=rsObj.getString("stu_name");
                String class=rsObj.getString("class");
                String dob=rsObj.getString("dob");
                String roll_no=rsObj.getString("roll_no");
                System.out.println(name+"\t");
                System.out.println(roll_no+"\t");
                System.out.println(dob+"\t");
                System.out.println(class);
            }
            conObj.close();
        }
        catch(Exception ex)
        {
            System.out.println("Error Occured");
            System.out.println("Error " + ex);
        }
    }
}

Save the above code as StudentInfo.java file.


Compile and Execute the application


The command to compile the StudentInfo application is:
      javac StudentInfo.java

The command to execute the StudentInfo application is:       
       java StudentInfo

SHARE THIS POST:

0 comments:

Post a Comment