INSERT AND VIEW OPERATION FOR STUDENT DETAILS USING DATABASE CONNECTIVITY



Aim:
            To implement insert and view operation using ActionEvent


ALGORITHM:
Step 1:             Start
Step 2:             Create a Frame for order details using Frame class.
Step 3:             Create the Text boxes and labels for the following fields,Regno,name,address.
Step 4:             Create the Save and View Buttons.
Step 5:             Connect the java application with database.
Step 6:             If the user clicks Save button, the system has to save all the text boxes
information into the database.
Step 7:             If the user gives the regno number and clicks the View button, the System has to
get all the textboxes information and view into the table.





PROGRAM:
import java.io.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.*;
class fra extends Frame implements ActionListener,WindowListener
{
MenuBar mb;
Menu Registration;
MenuItem Register;
fra(String str)
{
super(str);
mb=new MenuBar();
setBackground(Color.magenta);
setMenuBar(mb);
Registration=new Menu("Registration");
Register=new MenuItem("Register");
Registration.add(Register);
mb.add(Registration);
Registration.addActionListener(this);
addWindowListener(this);
}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){System.exit(0);}
public void windowClosing(WindowEvent e){System.exit(0);}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void actionPerformed(ActionEvent ae){
String msg=(String)ae.getActionCommand();
if(msg.equals("Register"))
{
try
{
Regs b=new Regs();
b.setVisible(true);
b.setTitle("Registration");
b.setSize(500,600);
}
catch(Exception e1){}
}
}
}
public class gasauto
{
public static void main(String args[])
{
fra s=new fra("Hello");
s.setVisible(true);
s.setSize(500,500);
}
}
class Regs extends Frame implements ActionListener,WindowListener
{
TextField Name,Regno,Address;
Button Save,view;
Connection con;
PreparedStatement ps,ps1,ps2;
Regs() throws Exception
{
Panel p1,p2,p3,p4,p5,p6,p7,p8,p9,p10;
setLayout(new GridLayout(10,1));
setFont(new Font("Monotype Corsiva",Font.BOLD,18));
Regno=new TextField(15);
Name=new TextField(15);
Address=new TextField(15);
Save=new Button("Save");
view=new Button("View");
Label l=new Label("Reg No");
Label l1=new Label("Name");
Label l2=new Label("Address");
p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();
p6=new Panel();
p7=new Panel();
p8=new Panel();
p9=new Panel();
p10=new Panel();
p1.add(l);
p1.add(Regno);
p2.add(l1);
p2.add(Name);
p3.add(l2);
p3.add(Address);
p4.setLayout(new GridLayout(2,4));
p4.add(Save);
p4.add(view);
add(p1);
add(p2);
add(p3);
add(p5);
add(p6);
add(p7);
add(p8);
add(p9);
add(p4);
add(p10);
Save.addActionListener(this);
view.addActionListener(this);
addWindowListener(this);
}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){setVisible(false);}
public void windowClosing(WindowEvent e){setVisible(false);}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void actionPerformed(ActionEvent ae){
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:samuel");
}catch(Exception q){System.out.println(q);}
if(ae.getSource()==Save)
{
try
{
ps=con.prepareStatement("insert into Table1 values (?,?,?)");
ps.setString(1,Regno.getText().trim());
ps.setString(2,Name.getText().trim());
ps.setString(3,Address.getText().trim());
ps.executeUpdate();
}
catch(Exception q)
{
System.out.println(q);
}}
if(ae.getSource()==view)
{
try
{
ps=con.prepareStatement("select * from Table1 where regno=?");
ps.setString(1,Regno.getText().trim());
rs=ps.executeQuery();
while(rs.next())
{
String s=rs.getString("Name");
Name.setText(s);
s=rs.getString("Address");
Address.setText(s);
}}catch(Exception q)
{System.out.println(q);
}}}}
OUTPUT:
SAVE

DATABASE:
Table1
Regno
Name
Address
08CA007
SARAVANA
SIVAKASI
24
ANTO
KRISHNAGIRI
25
VINO
SIVAKASI







VIEW



RESULT:
Thus the program has been executed and output is verified.

0 comments: