Database programming in java


This post will guide you to do programming in java to access database.

Before do the programming create a MS - Access database with your desired name. Then create a table and call it as student. Create a column and name it as "Name". Enter some of your friend names in the table.

Then goto control panel -> Administrative Tools -> Data Sources (ODBC). It will open a new window as given below 





Click on the button "Add". It will open a window as given below. Here we should select the driver which can communicate to the access database. So select the driver "Microsoft Access Driver (*.mdb). " and then click the button "Finish".















A new window will appear as given below


Here click on the button "Select". It will ask you select your database needs to be connected.











Then press ok. Then enter the name for your data source which will be used later in the program. Here I am giving the data source name as "student".













 Then press the "OK" button. It will add your data source in the User DSN list as given below














 Now we are ready to write the program. After compile the program given below if you run it, it will print your friends name in the command prompt (console window).

import java.sql.*;
public class Database
{
   public static void main (String args[])
   {
        try
        {
             class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             Connection conn = DriverManager.getConnection("jdbc:odbc:student");
             Statement s = conn.createStatement();
             Resultset rs = s.executeQuery("select * from student");
             while(rs.next())
             {
                     System.out.println(rs.getString("Name"));       
              }
           rs.close();
           conn.close();
       }
       catch (Exception ex)
       {
           System.out.println (ex.toString());
        }
}

Feel free to post your questions and your suggestions are welcome to make this blog very effective.

Thank You
P.Gopalakrishnan

Comments

Post a Comment

Popular posts from this blog

Hikoo's for Solarwinds Year End Bash

Tips to learn programming