A Java Program to implement address book.


 

Hey Friends!!! In this post we are going to implement address book as shown in above figure for that you need data base.You can use wamp as data base to download latest version of wamp check out our downloads section.

Requirements: Wamp Server
                          NetBeans IdE
                    Data Base
                                                                         Connection of NetBeans to wamp database.

Must Watch Before Starting Programming.

                                       A video tutorial to learn how to install Wamp Server
                    A video tutorial to learn creating database and a table in Wamp Server

A Video tutorial to connect a NetBeans to Wamp Sever. 


Let's start the coding of the address book.

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyFrame extends JFrame implements ActionListener
{
    JButton b1,b2,b3,b4,b5,b6;
    JTextField tx1,tx2;
    JPanel p1,p2,p3,p4;
    JLabel l1,l2;
    Statement st;
    Connection con;
    ResultSet rs;

    MyFrame()
    {
        p1=new JPanel();
        p2=new JPanel();
        p3=new JPanel();
        p4=new JPanel();
        l1=new JLabel("Name:");
        l2=new JLabel("Mo_no:");
        b1=new JButton("New");
        b2=new JButton("Add");
        b3=new JButton("First");
        b4=new JButton("Last");
        b5=new JButton("Next");
        b6=new JButton("Prev");

        tx1=new JTextField(10);

        tx2=new JTextField(10);
        add(p1);
        p1.setLayout(new GridLayout(3,1));
        p1.add(p2);
        p1.add(p3);
        p1.add(p4);
        p2.setLayout(new GridLayout(1,2));
        p2.add(l1);
        p2.add(tx1);
        p3.setLayout(new GridLayout(1,2));
        p3.add(l2);
        p3.add(tx2);
        p4.setLayout(new FlowLayout());
      
       

     
        p4.add(b1);p4.add(b2);p4.add(b3);
        p4.add(b4);p4.add(b5);p4.add(b6);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        try
        {
            Class.forName("com.mysql.jdbc.Driver");                  //installation of drivers 
            //Connection to database in wamp server.
            con =DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");    
             st = con.createStatement();
             rs=st.executeQuery("select * from abc");

        }
        catch(Exception e)
        {
           System.out.println(e);

        }
       
       


    }
    public void actionPerformed (ActionEvent e2)
    {
       
     String c=e2.getActionCommand();
    try
    {
        if(c.equals("New"))
        {
            tx1.setText(" ");
            tx2.setText(" ");
        }
        if(c.equals("Add"))
        {
        st.executeUpdate("insert into abc (Name,Mo_no) values('"+tx1.getText()+"','"+tx2.getText()+"')");
        tx1.setText(" ");
        tx2.setText(" ");
        rs = st.executeQuery("select * from abc");
        }
        if(c.equals("First"))
        {
        rs.first();
        tx1.setText(rs.getString(1));
        tx2.setText(rs.getString(2));


        }
        if(c.equals("Last"))
        {
          rs.last();
          tx1.setText(rs.getString(1));
          tx2.setText(rs.getString(2));

        }
        if(c.equals("Next"))
        {
         rs.next();
          tx1.setText(rs.getString(1));
          tx2.setText(rs.getString(2));
        }
        if(c.equals("Prev"))
        {
          rs.previous();
          tx1.setText(rs.getString(1));
          tx2.setText(rs.getString(2));
        }
    }
    catch(Exception e1)
    { System.out.println(e1);
    }
}
}
public class Main
{

                public static void main(String args[])
                {
                                MyFrame ob=new MyFrame();
                                ob.setSize(300,300);
                                ob.setVisible(true);
                }


}


Comments