Write a Java program to create a form with student id, student name, level, and two button insert and clear. Handle the event such that buttons with perform the operations as implied by their name.




 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Student extends JFrame implements ActionListener
{
 JLabel sid,sname,slevel;
 JTextField tid,tname, tlevel;
 JButton insert, clear;
 JPanel p1,p2,p3,p4;
 Student()
 {
  setSize(400,250);
  setTitle("Students Data Entry");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  p1=new JPanel();
  p2=new JPanel();
  p3=new JPanel();
  p4=new JPanel();

  setLayout(new BorderLayout());
  add(p1,BorderLayout.CENTER);
  add(p2,BorderLayout.SOUTH);
  p1.setLayout(new GridLayout(1,2));
  p1.add(p3);
  p1.add(p4);

  p3.setLayout(new FlowLayout(FlowLayout.LEFT, 75,20));
  p4.setLayout(new FlowLayout(FlowLayout.LEFT, 25,20));
  sid=new JLabel("Student ID");
  sname=new JLabel("Student Name");
  slevel=new JLabel("Level");
  p3.add(sid);
  p3.add(sname);
  p3.add(slevel);
  tid=new JTextField(10);
  tname=new JTextField(10);
  tlevel=new JTextField(10);
  p4.add(tid);
  p4.add(tname);
  p4.add(tlevel);
  p2.setLayout(new FlowLayout(FlowLayout.CENTER, 20,20));
  insert=new JButton("Insert");
  clear=new JButton("Clear");
  
                p2.add(insert);
  p2.add(clear);
  
  insert.addActionListener(this);
  clear.addActionListener(this);
    
  setVisible(true);
 }
 public void actionPerformed(ActionEvent ae)
 {
  Connection c=null;
  Statement s=null;
  try
  { 
   Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://localhost/mydb","root", "raj");
   s=c.createStatement();
   if(ae.getSource()==insert)
   {
           int id;
                                String name, level;
    id=Integer.parseInt(tid.getText());   
                                name=tname.getText();
    level=tlevel.getText();
    
  String query="insert into studentdb values("+id+",'"+name+"',"+level+")";
    s.executeUpdate(query);
  JOptionPane.showMessageDialog(this,"Data is Recorded!!!!");
   
   }
   
   if(ae.getSource()==clear)
   {
                            tid.setText("");  
                            tname.setText(""); 
                            tlevel.setText("");
   }
  }
  catch(Exception e)
  {
                    System.out.println(e);
  }
 }
 public static void main(String a[])
 {
  new Student();
 }
}