Friday, 18 January 2013

Insert, Update, Delete, View Operations in Oracle using Java JDBC

Here i am going to share how we can perform insert, update, delete and view operations on Oracle using Java JDBC...
Output Screens:









Step 1:
         We need some basic knowledge about Oracle database connectivity using Java,, if you know about that continue here.. otherwise if you need information   Refer here

Step 2:
         Create Table with follwing fields.
         Table Name : Employee
       
Column Name Column Type Description
empid Number Primary Key
empname Varchar
empsalary number

if you face any problem Refer Here

Step 3:
         Add required "ojdbc.jar" file into Eclipse Build path.
         For Your reference go here

Step 4:
         Create class DataModify into eclipse and paste following coding

DataModify
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class DataModify implements ActionListener
{
 
 JFrame mainframe=new JFrame("Database Operation");
 JPanel mainpanel=new JPanel();
 JButton insertbtn=new JButton("Insert");
 JButton viewbtn=new JButton("View");
 JButton modifybtn=new JButton("Modify");
 JButton deletebtn=new JButton("Delete");
 /* Variables For Insert Window*/
 JFrame insertframe=new JFrame("Insert into Database");
 JPanel insertpanel=new JPanel();
 JLabel empidlabel=new JLabel("Employee Id");
 JTextField empidtext=new JTextField(20);
 JLabel empnamelabel=new JLabel("Employee Name");
 JTextField empnametext=new JTextField(20);
 JLabel empsalarylabel=new JLabel("Employee Salary");
 JTextField empsalarytext=new JTextField(20);
 JButton insertsubmit=new JButton("Submit");
 //Variables for View Window
 JFrame viewframe=new JFrame("View Database Data");
 JPanel viewpanel=new JPanel();
 //Variables for Modify Window
 JFrame modifyframe=new JFrame("Modify the Data");
 JPanel modifypanel=new JPanel();
 JLabel mempidlabel=new JLabel("Employee Id");
 JTextField mempidtext=new JTextField(20);
 JLabel mempnamelabel=new JLabel("Employee Name");
 JTextField mempnametext=new JTextField(20);
 JLabel mempsalarylabel=new JLabel("Employee Salary");
 JTextField mempsalarytext=new JTextField(20);
 JButton modifysubmit=new JButton("Submit");
 //Variable for Delete Window
 JFrame deleteframe=new JFrame("Delete Data in Database");
 JPanel deletepanel=new JPanel();
 JLabel dempidlabel=new JLabel("Employee Id");
 JTextField dempidtext=new JTextField(20);
 JLabel dempnamelabel=new JLabel("Employee Name");
 JTextField dempnametext=new JTextField(20);
 JLabel dempsalarylabel=new JLabel("Employee Salary");
 JTextField dempsalarytext=new JTextField(20);
 JButton deletesubmit=new JButton("Submit");
 Connection con=null;
 Vector datacol=new Vector();
 Vector datarow=new Vector();
 JTable datatable;
 JScrollPane viewdata;
 
 public void init()
 {
  mainwindow();
  
  
 }
 public void mainwindow()
 {
  mainpanel.setBackground(Color.GRAY);
  mainpanel.setLayout(null);
  insertbtn.setBounds(300, 300, 100, 30);
  modifybtn.setBounds(450, 300, 100, 30);
  deletebtn.setBounds(600, 300, 100, 30);
  viewbtn.setBounds(800, 300, 100, 30);
  mainpanel.add(insertbtn);
  mainpanel.add(deletebtn);
  mainpanel.add(modifybtn);
  mainpanel.add(viewbtn);
  mainframe.add(mainpanel);
  mainframe.setSize(1350, 700);
  mainframe.setVisible(true);
  insertbtn.addActionListener(this);
  deletebtn.addActionListener(this);
  modifybtn.addActionListener(this);
  viewbtn.addActionListener(this);
  
  
 }
 public void insertwindow()
 {
 insertpanel.setBackground(Color.GRAY);
 empidlabel.setForeground(Color.WHITE);
 empnamelabel.setForeground(Color.WHITE);
 empsalarylabel.setForeground(Color.WHITE);
 insertpanel.setLayout(null);
 empidlabel.setBounds(400, 150, 100, 20);
 empidtext.setBounds(550, 150, 150, 30);
 empnamelabel.setBounds(400, 200, 100, 20);
 empnametext.setBounds(550, 200, 150, 30);
 empsalarylabel.setBounds(400, 250, 100, 20);
 empsalarytext.setBounds(550, 250, 150, 30);
 insertsubmit.setBounds(490, 300, 100, 30);
 
 insertpanel.add(empidlabel);
 
 insertpanel.add(empidtext);
 insertpanel.add(empnamelabel);
 insertpanel.add(empnametext);
 insertpanel.add(empsalarylabel);
 insertpanel.add(empsalarytext);
 insertpanel.add(insertsubmit);
 insertframe.add(insertpanel);
 
 insertframe.setSize(1350, 700);
 insertframe.setVisible(true);
 insertsubmit.addActionListener(this);
 insertframe.addWindowListener(new WindowAdapter()
 {
  public void windowClosing(WindowEvent e)
  {
   mainframe.setVisible(true);
  }
 }
 );
 }
 public void viewwindow()
 {
  
  datacol.removeAllElements();
  datarow.removeAllElements();
  OpenDatabase();
  try
  {
  Statement st=con.createStatement();
  ResultSet rs=st.executeQuery("select * from Employee");
  ResultSetMetaData rms=rs.getMetaData();
  int cols=rms.getColumnCount();
  for(int i=1;i<=cols;i++)
  {
   datacol.addElement(rms.getColumnName(i));
  }
  while(rs.next())
  {
   Vector row=new Vector(cols);
   for(int i=1;i<=cols;i++)
   {
    row.addElement(rs.getObject(i));
   }
   datarow.addElement(row);
  }
  datatable=new JTable(datarow,datacol);
  viewdata=new JScrollPane(datatable);
  
  }
  catch(Exception e)
  {
   JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
   
  }
  viewpanel.setBackground(Color.GRAY);
  viewpanel.setLayout(null);
  viewdata.setBounds(20, 50, 1250, 600);
  viewpanel.add(viewdata);
  viewframe.add(viewpanel);
  viewframe.setSize(1350, 700);
  viewframe.setVisible(true);
  viewframe.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    mainframe.setVisible(true);
   }
  });
  
  
  
  
 }
 public void modifywindow()
 {
  mempnametext.disable();
  mempsalarytext.disable();
  mempidtext.setText("");
  mempnametext.setText("");
  mempsalarytext.setText("");
  modifypanel.setBackground(Color.GRAY);
  mempidlabel.setForeground(Color.WHITE);
  mempnamelabel.setForeground(Color.WHITE);
  mempsalarylabel.setForeground(Color.WHITE);
  modifypanel.setLayout(null);
  mempidlabel.setBounds(400, 150, 100, 20);
  mempidtext.setBounds(550, 150, 150, 30);
  mempnamelabel.setBounds(400, 200, 100, 20);
  mempnametext.setBounds(550, 200, 150, 30);
  mempsalarylabel.setBounds(400, 250, 100, 20);
  mempsalarytext.setBounds(550, 250, 150, 30);
  modifysubmit.setBounds(490, 300, 100, 30);
  
  modifypanel.add(mempidlabel);
  
  modifypanel.add(mempidtext);
  modifypanel.add(mempnamelabel);
  modifypanel.add(mempnametext);
  modifypanel.add(mempsalarylabel);
  modifypanel.add(mempsalarytext);
  modifypanel.add(modifysubmit);
  modifyframe.add(modifypanel);
  
  modifyframe.setSize(1350, 700);
  modifyframe.setVisible(true);
  modifysubmit.addActionListener(this);
  modifyframe.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    mainframe.setVisible(true);
   }
  });
  mempidtext.addActionListener(this);
  
 }
 public void deletewindow()
 {
  dempnametext.disable();
  dempsalarytext.disable();
  dempidtext.setText("");
  dempnametext.setText("");
  dempsalarytext.setText("");
  deletepanel.setBackground(Color.GRAY);
  dempidlabel.setForeground(Color.WHITE);
  dempnamelabel.setForeground(Color.WHITE);
  dempsalarylabel.setForeground(Color.WHITE);
  deletepanel.setLayout(null);
  dempidlabel.setBounds(400, 150, 100, 20);
  dempidtext.setBounds(550, 150, 150, 30);
  dempnamelabel.setBounds(400, 200, 100, 20);
  dempnametext.setBounds(550, 200, 150, 30);
  dempsalarylabel.setBounds(400, 250, 100, 20);
  dempsalarytext.setBounds(550, 250, 150, 30);
  deletesubmit.setBounds(490, 300, 100, 30);
  
  deletepanel.add(dempidlabel);
  
  deletepanel.add(dempidtext);
  deletepanel.add(dempnamelabel);
  deletepanel.add(dempnametext);
  deletepanel.add(dempsalarylabel);
  deletepanel.add(dempsalarytext);
  deletepanel.add(deletesubmit);
  deleteframe.add(deletepanel);
  
  deleteframe.setSize(1350, 700);
  deleteframe.setVisible(true);
  deletesubmit.addActionListener(this);
  dempidtext.addActionListener(this);
  deleteframe.addWindowListener(new WindowAdapter()
  {
   public void windowClosing(WindowEvent e)
   {
    mainframe.setVisible(true);
   }
  });  
  
 }
 public void insertdata(int eid, String ename, int esalry)
 {
  try
  {
  Statement st=con.createStatement();
  st.executeUpdate("insert into Employee values("+eid+",'"+ename+"',"+esalry+")");
  JOptionPane.showConfirmDialog(null, "Your Data Has been Inserted", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE);
  } 
  catch(Exception e)
  {
   JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
  }
 }
 public void deletedata(int eid)
 {
  try
  {
   Statement st=con.createStatement();
   st.executeUpdate("delete from Employee where empid="+eid);
   JOptionPane.showConfirmDialog(null, "Your Data Has been Deleted", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE);
  }
  catch(Exception e)
  {
   JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
  }
 }
 public void modifydata(int eid, String ename, int esal)
 {
  try
  {
   Statement st=con.createStatement();
   st.executeUpdate("update Employee set empname='"+ename+"', empsalary="+esal+" where empid="+eid);
   JOptionPane.showConfirmDialog(null, "Your Data Has been Modified", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.PLAIN_MESSAGE);
  }
  catch(Exception e)
  {
   JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
  }
 }
 public void OpenDatabase()
 {
  
  try
  {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","Partha","sarathi");
   
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
 public void CloseDatabase()
 {
  try {
   con.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 @Override
 public void actionPerformed(ActionEvent arg0) {
  // TODO Auto-generated method stub
  if(arg0.getSource()==insertbtn)
  {
   mainframe.setVisible(false);
   insertwindow();
  }
  if(arg0.getSource()==viewbtn)
  {
   mainframe.setVisible(false);
   viewwindow();
  }
  if(arg0.getSource()==modifybtn)
  {
   mainframe.setVisible(false);
   modifywindow();
  }
  if(arg0.getSource()==deletebtn)
  {
   mainframe.setVisible(false);
   deletewindow();
  }
  if(arg0.getSource()==insertsubmit)
  {
   int empid=Integer.parseInt(empidtext.getText());
   String ename=empnametext.getText();
   int esalary=Integer.parseInt(empsalarytext.getText());
   try
   {
   OpenDatabase();
   insertdata(empid,ename,esalary);
   
   CloseDatabase();
   
   }
   catch(Exception e)
   {
    System.out.println(e);
   }
   empidtext.setText("");
   empnametext.setText("");
   empsalarytext.setText("");
  
   
   
  }
  if(arg0.getSource()==modifysubmit)
  {
   int empid=Integer.parseInt(mempidtext.getText());
   String ename=mempnametext.getText();
   int esalary=Integer.parseInt(mempsalarytext.getText());
   OpenDatabase();
   modifydata(empid,ename,esalary);
   CloseDatabase();
  }
  if(arg0.getSource()==deletesubmit)
  {
   int eid=Integer.parseInt(dempidtext.getText());
   OpenDatabase();
   deletedata(eid);
   CloseDatabase();
   eid=0;
  }
  if(arg0.getSource()==mempidtext)
  {
   int eid=Integer.parseInt(mempidtext.getText());
   try
   {
   OpenDatabase();
   Statement st=con.createStatement();
   ResultSet rs=st.executeQuery("Select * from Employee where empid="+eid);
   if(rs.next())
   {
   mempnametext.setText(rs.getString("empname"));
   mempsalarytext.setText(String.valueOf(rs.getInt("empsalary")));
   }
   CloseDatabase();
   mempnametext.enable();
   mempsalarytext.enable();
   }
   catch(Exception e)
   {
 
    JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
   }
  }
  if(arg0.getSource()==dempidtext)
  {
   int eid=Integer.parseInt(dempidtext.getText());
   try
   {
    OpenDatabase();
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("Select * from Employee where empid="+eid);
    if(rs.next())
    {
    dempnametext.setText(rs.getString("empname"));
    dempsalarytext.setText(String.valueOf(rs.getInt("empsalary")));
    }
    CloseDatabase();
    
    
   }
   catch(Exception e)
   {
    JOptionPane.showConfirmDialog(null, "Problem in Database connectivity or Data", "Result", JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE);
   }
  }
  
 }
 public static void main(String args[])
 {
  DataModify dm=new DataModify();
  dm.init();
  
 }
  
}
Step 5:
Change the username and Password for Database...
replace your username for "testuser" and replace your password for "test"


Thanks for Visiting....

6 comments:

  1. Hey Good..
    Here After Please Give .java file insted of Full programs...


    ReplyDelete
  2. when i execute above program following errors
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:

    at DataModify.actionPerformed(DataModify.java:340)
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:

    at DataModify.actionPerformed(DataModify.java:340)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    can u ple help meto solve this

    ReplyDelete
  3. How to connect data modify page by a button

    ReplyDelete
  4. very good example sir
    i like it
    but insert a image lable

    ReplyDelete
  5. i alter the code you posted, every time i try to update, it allways go to insert data and not the update data

    ReplyDelete