Friday, July 20, 2018

JFrame connect with Database

import java.sql.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class odframe extends JFrame implements ActionListener
{
        Connection c;
        Statement st;
        ResultSet rs;
        JFrame f;
        TextField t1,t2,t3;
        Button b1,b2,b3,b4,b5,b6,b7;
        Label l1,l2,l3;
        Panel p;
        TextArea ta;

 public void actionPerformed(ActionEvent ae1)
 {
     Button bt=(Button)ae1.getSource();
       if(bt.getLabel().equals("Load"))
         Load();
 
       if(bt.getLabel().equals("Next"))
         Next();
 
        if(bt.getLabel().equals("Get"))
         Get();
 
        if(bt.getLabel().equals("Add"))
         Add();
 
        if(bt.getLabel().equals("Delete"))
         Delete();
       
        if(bt.getLabel().equals("Clear"))
                         Clear();

        if(bt.getLabel().equals("Update"))
       
                Update();
       
  }
 
  public odframe()
  {
           f=new JFrame("file server");
        f.setSize(320,300);
        f.setBackground(Color.gray);
        f.setForeground(Color.magenta);
        p=new Panel();
        b1=new Button("Load");
        b2=new Button("Next");
        b3=new Button("Get");
        b4=new Button("Update");
        b5=new Button("Add");
        b6=new Button("Delete");
        b7=new Button("Clear");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        t1=new TextField("       ");
        t2=new TextField("       ");
        t3=new TextField("       ");
        l1=new Label("Empno");
        l2=new Label("Ename");
        l3=new Label("Salary");
        ta=new TextArea(10,30);
        f.add(p);
        p.add(l1);
        p.add(t1);
        p.add(l2);
        p.add(t2);
        p.add(l3);
        p.add(t3);
           p.add(b1);
           p.add(b2);
           p.add(b3);
           p.add(b4);
           p.add(b5);
           p.add(b6);
           p.add(b7);
           p.add(ta);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  void Load()
  {
        try
        {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     c=DriverManager.getConnection("jdbc:odbc:connect_ora","scott","tiger");
     st=c.createStatement();

     rs=st.executeQuery("select *from appu");
     //rs.setFetchDirection(ResultSet.TYPE_SCROLL_SENSITIVE);

        rs.next();
        t1.setText(rs.getString("empno"));
        t2.setText(rs.getString("ename"));
        t3.setText(rs.getString("sal"));
       }
        catch(Exception e)
        {
        e.toString();
        }
   }

   public void Next()
   {
        try
        {
        rs.next();
        t1.setText(rs.getString("empno"));
        t2.setText(rs.getString("ename"));
        t3.setText(rs.getString("sal"));
        }
        catch(Exception e)
        {
        ta.append("No Next Record...");
        }
   }
   public void Get()
   {
        try
        {
        rs=st.executeQuery("select *from appu where empno="+t1.getText());
        rs.next();
        t1.setText(rs.getString("empno"));
        t2.setText(rs.getString("ename"));
        t3.setText(rs.getString("sal"));
        }
        catch(Exception e)
        {
        ta.append("No Prev Record...");
        ta.append(e.toString());
           }
   }

   public void Add()
   {
   try
   {
   rs=st.executeQuery("insert into appu values("+t1.getText()+",'"+t2.getText()+"',"+t3.getText()+")");
   }
        catch(Exception e)
        {
        ta.append(e.toString());
        }
  }
   public void Clear()
   {
        try
        {
        rs.next();
        t1.setText(" ");
        t2.setText(" ");
        t3.setText(" ");
        }
        catch(Exception e)
        {
        ta.append(e.toString());
        }
    }
   public void Delete()
   {
        try
        {
        rs=st.executeQuery("delete from appu where empno="+t1.getText());
        }
        catch(Exception e)
        {
        ta.append(e.toString());
        }
    }
   public void Update()
   {
        try
        {
        rs=st.executeQuery("update appu set ename="+"'"+t2.getText()+"' where empno="+t1.getText());
        }
        catch(Exception e)
        {
        ta.append(e.toString());
        }
    }

   public static void main(String args[])
   {
    new odframe();
    }
 } 

No comments:

Post a Comment