import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class StaffRecord extends JFrame implements ActionListener {
JLabel heading,id,Name,Dept,Year,Handlingsubject,empty_la bel,error;
JTextField idField, NameField, DeptField,YearField, HandlingsubjectField;
JButton insert,update,delete,clear,exit;
GridBagLayout gb1;
GridBagConstraints gbc;
JComboBox idCombo;
Connection con;
PreparedStatement stat;
Statement stmt;
ResultSet rs;
Font f;
JPanel jp1;
public StaffRecord() throws ClassNotFoundException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT s_id FROM Records");
while (rs.next())
{
idCombo.addItem(Integer.toString(rs.getInt(1)));
}
con.close();
}
catch(Exception e)
{
System.out.println("Error : " +e);
}
idCombo.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand()=="Exit");
System.exit(0);
if(ae.getActionCommand()=="Delete");
{
try
{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
stat=con.prepareStatement("Delete from Records WHERE s_id =?");
String selected_id =idCombo.getSelectedItem().toString();
int id = Integer.parseInt(selected_id);
stat.setInt(1, id);
stat.executeUpdate();
con.close();
idCombo.removeActionListener(this);;
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT s_id FROM Records");
idCombo.removeAllItems();
while(rs.next())
idCombo.addItem(Integer.toString(rs.getInt(1)));
con.close();
idCombo.addActionListener(this);
idField.setText("");
NameField.setText("");
DeptField.setText("");
YearField.setText("");
HandlingsubjectField.setText("");
error.setText("Row Deleted");
}
catch(Exception e)
{
System.out.println("Error"+e);
error.setText("Deletion Failed --Retry");
}
}
if(ae.getActionCommand()=="Insert")
{
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
stat = con.prepareStatement("INSERT INTO Records VALUES(?,?,?,?,?");
String id=idField.getText();
String Name=NameField.getText();
String Dept=DeptField.getText();
String Year=YearField.getText();
String Handlingsubject=HandlingsubjectField.getText();
stat.setInt(1,Integer.parseInt(id));
stat.setString(2,Name);
stat.setString(3,Dept);
stat.setInt(4,Integer.parseInt(Year));
stat.setString(5, Handlingsubject);
stat.executeUpdate();
con.close();
idCombo.removeActionListener(this);
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
stmt =con.createStatement();
rs=stmt.executeQuery("SELECT s_id FROM Records");
idCombo.removeAllItems();
while(rs.next())
idCombo.addItem(Integer.toString(rs.getInt(1)));
con.close();
idCombo.addActionListener(this);
error.setText("Row Inserted");
}
catch(Exception e)
{
System.out.println("Error1 :"+e);
error.setText("Insertion Failed --Retry");
}
}
if(ae.getSource()==idCombo)
{
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
String selected_id=idCombo.getSelectedItem().toString();
int id=Integer.parseInt(selected_id);
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT s_Name,s_Dept,s_Year,s_Handlingsubject");
rs.next();
idField.setText(selected_id);
NameField.setText(rs.getString(1));
DeptField.setText(rs.getString(2));
YearField.setText(Integer.toString(rs.getInt(3)));
HandlingsubjectField.setText(rs.getString(4));
con.close();
}
catch(Exception e)
{
System.out.println("Error :"+e);
}
}
if(ae.getActionCommand()=="Update");
{
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mystaffrecord","root","admin");
stat=con.prepareStatement("UPDATE Records SET s_Name =?,s_Dept=?,s_Year=?,s_Handlingsubject=? where s_id=?");
String id=idField.getText();
String Name=NameField.getText();
String Dept=DeptField.getText();
String Year=YearField.getText();
String Handlingsubject=HandlingsubjectField.getText();
stat.setInt(1,Integer.parseInt(id));
stat.setString(2,Name);
stat.setString(3,Dept);
stat.setInt(4,Integer.parseInt(Year));
stat.setString(5, Handlingsubject);
stat.executeUpdate();
con.close();
error.setText("Row Updated");
}
catch(Exception e)
{
System.out.println("Error :"+e);
error.setText("Row updation Failure");
}
}
if(ae.getActionCommand()=="Clear")
{
idField.setText("");
NameField.setText("");
DeptField.setText("");
YearField.setText("");
HandlingsubjectField.setText("");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) throws ClassNotFoundException
{
StaffRecord s= new StaffRecord();
s.setVisible(true);
}
}