JAVA INSERT AND VIEW OPERATION FOR STUDENT DETAILS USING DATABASE CONNECTIVITY


PROCEDURE:
1)     Start the program.
2)     Create a class frame that extends frame and implements ActionListener interface.
3)     Create objects for MenuBar, Menu, and MenuItem.
4)     Include MenuBar in the frame.
5)     In the exno10 class, include all the methods of the actionListener interface.
6)     In the actionPerformed(), connect to the access database using mdb.
7)     Create a database in access and create a table with the fields regno,name,address,phoneno.
8)      Enter the values in the text fields and on clicking save, it is saved to  the  database.
9)      Enter the clicking view, the details are retrieved from the database and displayed.
10)Stop the program.


SOURCE CODE:
import java.awt.*;
import java.sql.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
class frame extends Frame implements ActionListener
{
Frame f;
            frame(String title,Frame f)
            {
                        super(title);
                        MenuBar mb=new MenuBar();
                        setMenuBar(mb);
                        Menu mr=new Menu("Registration");
                        MenuItem mi=new MenuItem("Register");
                        MenuItem m1=new MenuItem("-");
                        MenuItem m=new MenuItem("Exit");
                        mb.add(mr);
                        mr.add(mi);
                        mr.add(m1);
                        mr.add(m);
                        mi.addActionListener(this);
                        m.addActionListener(this);
                        this.f=f;
                       
            }
            public void actionPerformed(ActionEvent ae)
            {
                        String str=ae.getActionCommand();
                        if(str.equals("Register"))
                                    f.setVisible(true);
                        if(str.equals("Exit"))
                                    System.exit(0);
            }
}
class frame1 extends Frame implements ActionListener
{
            TextField tname;
            TextField tregister;
            TextField taddress;
            TextField tphone;
            static int count=1;
            frame1()
            {
                        super("Form_frame");
                        setLayout(null);
Label b=new Label("Name");
                        b.setBounds(22,35,60,36);
                        add(b);
                        tname=new TextField(30);
                        tname.setBounds(120,40,200,30);
                        add(tname);

                        Label l1=new Label("Register No");
                        l1.setBounds(22,70,70,40);
                        add(l1);
                        tregister=new TextField(30);
                        tregister.setBounds(120,75,200,30);
                        add(tregister);
                        Label l2=new Label("Address");
                        l2.setBounds(22,110,70,36);
                        add(l2);
                        taddress=new TextField(30);
                        taddress.setBounds(120,115,200,30);
                        add(taddress);
                        Label l3=new Label("Phone No");
                        l3.setBounds(22,150,70,30);
                        add(l3);
                        tphone=new TextField(30);
                        tphone.setBounds(120,150,200,30);
                        add(tphone);
                        Button bt=new Button("Save");
                        bt.setBounds(60,200,70,30);
                        bt.addActionListener(this);
                        add(bt);
                        Button bt1=new Button("View");
                        bt1.setBounds(150,200,70,30);
                        bt1.addActionListener(this);
                        add(bt1);

                        Button bt2=new Button("Clear");
                        bt2.setBounds(240,200,70,30);
                        bt2.addActionListener(this);
                        add(bt2);
            }
            public void actionPerformed(ActionEvent ae)
            {
                        Statement stmt=null;
                        Connection connection=null;
                        ResultSet rs = null;
                        StringBuffer sql=null;
                        String str=ae.getActionCommand();
                        try
                        {
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    connection=DriverManager.getConnection("jdbc:odbc:kanagaraj");
                        }
                        catch(Exception e)
                        {
                                    e.printStackTrace();
                        }
            if(str.equals("Save"))
            {
                        try
                        {
stmt=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                                      ResultSet.CONCUR_READ_ONLY);
                                    sql=new StringBuffer("Insert into kanaga values(");
                                    sql.append("'");
                                    sql.append(tname.getText());
                                    sql.append("',");
                                    sql.append("'");
                                    sql.append(tregister.getText());
                                    sql.append("',");
                                    sql.append("'");
                                    sql.append(taddress.getText());
                                    sql.append("',");
                                    sql.append("'");
                                    sql.append(tphone.getText());
                                    sql.append("')");
                                    boolean status=stmt.execute(sql.toString());
                                    System.out.println("Record Saved Sucessfully");
                                    stmt.close();
                                    connection.close();
                        }
                        catch(Exception e)
                        {
                                    e.printStackTrace();
                        }
            }
            else if(str.equals("View"))
            {
                        try
                        {
                        stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                                                                ResultSet.CONCUR_READ_ONLY);
                                    rs = stmt.executeQuery("select * from kanaga");
                                    rs.last();
                                    int rowCount=rs.getRow();
                                    if(count>rowCount)
                                    {
JOptionPane.showMessageDialog(null,"No more Records in Database","Aborting...",JOptionPane.INFORMATION_MESSAGE);
                                    }
                                    rs.beforeFirst();
                                    rs.absolute(count);
                                    tname.setText(rs.getString(1));
                                    tregister.setText(rs.getString(2));
                                    taddress.setText(rs.getString(3));
                                    tphone.setText(rs.getString(4));
                                    System.out.println("Retrived");
                                    rs.close();
                                    stmt.close();
                                    connection.close();
                                    count=count+1;
                        }
                        catch(Exception e)
                        {
                                    e.printStackTrace();
                        }
            }
            else if(str.equals("Clear"))
            {
                        tname.setText("");
                        tregister.setText("");
                        taddress.setText("");
                        tphone.setText("");
            }
            }
}
           
public class exno10
{
            public static void main(String args[])
            {
                        frame1 f=new frame1();
                        frame f1=new frame("Registration",f);
                        f1.setVisible(true);
                        f1.setSize(500,500);
                        f.setVisible(false);
                        f.setSize(400,400);
            }
}
OUTPUT

0 comments: