3, ఏప్రిల్ 2012, మంగళవారం

A SMALL APPLICATION WHICH WILL GIVE U AN IDEA HOW TO DEVELOP APPLICATIONS USING CORE JAVA


Note :- A SMALL APPLICATION WHICH WILL GIVE U AN IDEA HOW TO DEVELOP APPLICATIONS USING CORE JAVA CONCEPTS BUT THIS PROGRAM DID NOT USE THE TOOLS IF WE USE TOOLS FOR THIS CLASS DOCUMENTATION WILL BE ADDED ACCORDING TO SE APPROACH
AND ALSO ONE MORE THING THE BELOW USER INTERFACES GENERALLY ARE WRITTEN BY USING HTML,DHTML,CSS,JAVA SCRIPT AND SO ON BUT I FRAMED FOR THIS LEARNING PURPOSE


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

// CLASS-1 USER INTERFACE DESIGN   IT CONTAINS THE FIELDS USERNAME, PASSWORD AND 2 BUTTONS OK, CANCEL
class Login extends JFrame implements ActionListener
{
       JLabel un,pwd;
       JPasswordField tfpwd;
       JTextField tfun;
       JButton ok,cancel;
       Login(String s)
       {
              super(s);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Container c=getContentPane();
              c.setLayout(null);
              un=new JLabel("User Name : ");
              pwd=new JLabel("Password : ");
              un.setForeground(Color.blue);
              pwd.setForeground(Color.blue);
              un.setBounds(100,100,100,30);
              pwd.setBounds(100,200,100,30);
              c.add(un);
              c.add(pwd);
              tfun=new JTextField();
              tfpwd=new JPasswordField();
              tfun.setForeground(Color.blue);
              tfpwd.setForeground(Color.blue);
              tfun.setBounds(250,100,100,30);
              tfpwd.setBounds(250,200,100,30);
              c.add(tfun);
              c.add(tfpwd);
              ok=new JButton("OK");
              cancel=new JButton("CANCEL");
              ok.setBackground(Color.blue);
              cancel.setBackground(Color.blue);
              ok.setForeground(Color.yellow);
              cancel.setForeground(Color.yellow);
              ok.setBounds(100,400,100,30);
              cancel.setBounds(250,400,100,30);
              c.add(ok);
              c.add(cancel);
              ok.addActionListener(this);
              cancel.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String s=ae.getActionCommand();
              if(s.equals("OK"))
              {
                     String s1,s2;
                     s1=tfun.getText();
                     s2=tfpwd.getText();
                     if((s1.equals("Employee"))&&(s2.equals("Employee")))
                     {
                           dispose();
                           Options o=new Options("OPTIONS WINDOW");
                           o.show();
                           o.setSize(800,600);
                     }
              }
              else if(s.equals("CANCEL"))
                     System.exit(0);
       }
}

// THIS CLASS IT WILL PROVIDE SOME OPTATIONS ADD OPEN CLOSE UPDATE GENERATE SALARY EXIT CREATE ALL THESE FOR THE TABLE MANIPULATION IN THE DATABASE
class Options extends JFrame implements ActionListener
{
       JMenuBar mb;
       JMenu options,database;
       JMenuItem addnew,open,close,generatesal,exit,create,update;
       Options(String s)
       {
              super(s);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Container c=getContentPane();
              c.setLayout(null);
              mb=new JMenuBar();
              setJMenuBar(mb);
              options=new JMenu("Options");
              database=new JMenu("DataBase");
              options.setBackground(Color.blue);
              options.setForeground(Color.yellow);
              database.setBackground(Color.blue);
              database.setForeground(Color.yellow);
              open=new JMenuItem("Open");
              close=new JMenuItem("Close");
              generatesal=new JMenuItem("Genarate Salary");
              exit=new JMenuItem("Exit");
              options.setMnemonic('o');
              database.setMnemonic('d');
              mb.add(options);
              mb.add(database);
              addnew=new JMenuItem("Add New");
              addnew.setMnemonic('a');
              open.setMnemonic('o');
              close.setMnemonic('c');
              generatesal.setMnemonic('g');
              exit.setMnemonic('x');
              options.add(addnew);
              options.add(open);
              options.add(close);
              options.addSeparator();
              options.add(generatesal);
              options.addSeparator();
              options.add(exit);
              create=new JMenuItem("CREATE");
              update=new JMenuItem("UPDATE");
              create.setMnemonic('c');
              update.setMnemonic('u');
              database.add(create);
              database.add(update);
              create.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String s=ae.getActionCommand();
              if(s.equals("CREATE"))
              {
                     dispose();
                     CreateWindow cw=new CreateWindow("CREATE WINDOW");
                     cw.show();
                     cw.setSize(800,600);
              }
       }
}
class CreateWindow extends JFrame implements ActionListener
{
       JLabel tname,nf;
       JTextField tftname,tfnf;
       JButton next,cancel;
       CreateWindow(String s)
       {
              super(s);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Container c=getContentPane();
              c.setLayout(null);
              tname=new JLabel("Table Name : ");
              nf=new JLabel("Number Of Fields : ");
              tname.setForeground(Color.blue);
              nf.setForeground(Color.blue);
              tname.setBounds(100,100,300,30);
              nf.setBounds(100,400,300,30);
              c.add(tname);
              c.add(nf);
              tftname=new JTextField();
              tfnf=new JTextField();
              tftname.setForeground(Color.blue);
              tfnf.setForeground(Color.blue);
              tftname.setBounds(400,100,100,30);
              tfnf.setBounds(400,400,100,30);
              c.add(tftname);
              c.add(tfnf);
              next=new JButton("NEXT");
              cancel=new JButton("CANCEL");
              next.setBackground(Color.blue);
              cancel.setBackground(Color.blue);
              next.setForeground(Color.yellow);
              cancel.setForeground(Color.yellow);
              next.setBounds(200,500,100,30);
              cancel.setBounds(400,500,100,30);
              c.add(next);
              c.add(cancel);
              next.addActionListener(this);
              cancel.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String s=ae.getActionCommand();
              if(s.equals("NEXT"))
              {
                     dispose();
                     TableDetails td=new TableDetails("TABLE DETAILS WINDOW",tftname.getText(),Integer.parseInt(tfnf.getText()));
                     td.show();
                     td.setSize(800,600);
              }
              else if(s.equals("CANCEL"))
              {
                     dispose();
                     TableDetails td=new TableDetails("TABLE DETAILS WINDOW","student",4);
                     td.show();
                     td.setSize(800,600);
              }
       }
}


class TableDetails extends JFrame implements ActionListener
{
       String tname;
       int nf;
       JTextField tf[];
       JComboBox cb[];
       JLabel la[];
       JButton submit,cancel;
       JLabel l,tnl;
       String fname[],ftype[];
       TableDetails(String s,String tn,int n)
       {
              super(s);
              tname=tn;
              nf=n;
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Container c=getContentPane();
              c.setLayout(null);
              l=new JLabel("Table       Name    : ");
              l.setForeground(Color.blue);
              l.setBounds(100,100,200,30);
              c.add(l);
              tnl=new JLabel(""+tn);
              tnl.setForeground(Color.blue);
              tnl.setBounds(300,100,200,30);
              c.add(tnl);
              tf=new JTextField[n];
              cb=new JComboBox[n];
              la=new JLabel[n];
              for(int i=0;i<nf;i++)
              {
                     tf[i]=new JTextField();
                     cb[i]=new JComboBox();
                     cb[i].addItem("Number");
                     cb[i].addItem("Varchar2");
                     tf[i].setForeground(Color.blue);
                     cb[i].setForeground(Color.blue);
                     tf[i].setBounds(100,200+i*30,100,30);
                     cb[i].setBounds(300,200+i*30,100,30);
                     c.add(tf[i]);
                     c.add(cb[i]);
                     la[i]=new JLabel("Field "+(i+1));
                     la[i].setBounds(50,200+i*30,100,30);
                     c.add(la[i]);
              }
              submit=new JButton("SUBMIT");
              submit.setBackground(Color.blue);
              submit.setForeground(Color.yellow);
              submit.setBounds(500,200,100,30);
              c.add(submit);
              cancel=new JButton("CANCEL");
              cancel.setBackground(Color.blue);
              cancel.setForeground(Color.yellow);
              cancel.setBounds(500,300,100,30);
              c.add(cancel);
              fname=new String[n];
              ftype=new String[n];
              submit.addActionListener(this);
              cancel.addActionListener(this);
       }
      


public void actionPerformed(ActionEvent ae)
       {
              String s=ae.getActionCommand();
              if(s.equals("SUBMIT"))
              {
                     int i;
                     for(i=0;i<nf;i++)
                     {
                           fname[i]=tf[i].getText();
                            ftype[i]=(String)cb[i].getSelectedItem();
                           System.out.println("\n......"+fname[i]+"\t"+ftype[i]);
                           tf[i].setText("");
                     }
                     try
                     {
                           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                           Connection c=DriverManager.getConnection("Jdbc:Odbc:sample","scott","tiger");
                           Statement stmt=c.createStatement();
                           String command="create table "+tname+"(";
                           for(i=0;i<nf-1;i++)
                           {
                                  if(ftype[i].equals("Varchar2"))
                                         command=command+fname[i]+" "+ftype[i]+"(25),";
                                  else
                                         command=command+fname[i]+" "+ftype[i]+",";
                           }
                           if(ftype[i].equals("Varchar2"))
                                  command=command+fname[i]+" "+ftype[i]+"(25))";
                           else
                                  command=command+fname[i]+" "+ftype[i]+")";
                           System.out.println("\n....."+command);
                           stmt.execute(command);
              JOptionPane.showMessageDialog(null,"TABLE CREATED SUCCESSFULLY","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
                           c.close();
                     }
                     catch(Exception e)
                     {
                           JOptionPane.showMessageDialog(null,"Error In Connection","ERROR",JOptionPane.ERROR_MESSAGE);
                     }
                     String yes=JOptionPane.showInputDialog(null,"Do u Want To Inert","Insert",JOptionPane.QUESTION_MESSAGE);
                     if(yes.equals("yes"))
                     {
                           dispose();
                           ReadWindow rw=new ReadWindow("READ WINDOW",tname);
                           rw.show();
                           rw.setSize(800,600);
                     }
              }
              else if(s.equals("CANCEL"))
              {
                     dispose();
                     Options o=new Options("OPTIONS WINDOW");
                     o.show();
                     o.setSize(800,600);
              }
       }      }
class ReadWindow extends JFrame implements ActionListener
{
       String tname;
       JTextField tf[];
       JComboBox cb[];
       JLabel la[];
       JButton insert,cancel;
       JLabel l,tnl;
       ReadWindow(String s,String tn)
       {
              super(s);
              tname=tn;
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Container c=getContentPane();
              c.setLayout(null);
              l=new JLabel("Table       Name    : ");
              l.setForeground(Color.blue);
              l.setBounds(100,100,200,30);
              c.add(l);
              tnl=new JLabel(""+tn);
              tnl.setForeground(Color.blue);
              tnl.setBounds(300,100,200,30);
              c.add(tnl);
              insert=new JButton("INSERT");
              insert.setBackground(Color.blue);
              insert.setForeground(Color.yellow);
              insert.setBounds(500,200,100,30);
              c.add(insert);
              cancel=new JButton("CANCEL");
              cancel.setBackground(Color.blue);
              cancel.setForeground(Color.yellow);
              cancel.setBounds(500,300,100,30);
              c.add(cancel);
              try
              {
                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                     Connection con=DriverManager.getConnection("Jdbc:Odbc:sample","scott","tiger");
                     Statement stmt=con.createStatement();
                     ResultSet rs=stmt.executeQuery("Select * from "+tname);
                     ResultSetMetaData rm=rs.getMetaData();
                     int col=rm.getColumnCount();
                     tf=new JTextField[col+1];
                     la=new JLabel[col+1];
                     for(int i=1;i<=col;i++)
                     {
                           tf[i]=new JTextField();
                           tf[i].setForeground(Color.blue);
                           tf[i].setBounds(400,200+i*30,100,30);
                           c.add(tf[i]);
                           la[i]=new JLabel(rm.getColumnName(i)+"=====>"+rm.getColumnTypeName(i));
                           la[i].setBounds(50,200+i*30,100,30);
                           c.add(la[i]);
                     }
                     con.close();
              }
              catch(Exception e)
              {
                     JOptionPane.showMessageDialog(null,"Error In Connection","ERROR",JOptionPane.ERROR_MESSAGE);
              }
              insert.addActionListener(this);
              cancel.addActionListener(this);
       }
       public void actionPerformed(ActionEvent ae)
       {
              String s=ae.getActionCommand();
              if(s.equals("INSERT"))
              {
                     int i;
                     try
                     {
                           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                           Connection c=DriverManager.getConnection("Jdbc:Odbc:sample","scott","tiger");
                           Statement stmt=c.createStatement();
                           ResultSet rs=stmt.executeQuery("Select * from "+tname);
                           ResultSetMetaData rm=rs.getMetaData();
                           int col=rm.getColumnCount();
                           String command="insert into "+tname+" values(";
                           for(i=1;i<col;i++)
                           {
                                  if(rm.getColumnTypeName(i).equals("varchar2"))
                                         command=command+"'"+tf[i].getText()+"',";
                                  else
                                         command=command+tf[i].getText()+",";
                            }
                           if(rm.getColumnTypeName(i).equals("varchar2"))
                                  command=command+"'"+tf[i].getText()+"')";
                           else
                                  command=command+tf[i].getText()+")";
                           stmt.execute(command);
JOptionPane.showMessageDialog(null,"SUCCESSFULLYINSERTED","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
                           c.close();
                     }
                     catch(Exception e)
                     {
                           JOptionPane.showMessageDialog(null,"Error In Connection","ERROR",JOptionPane.ERROR_MESSAGE);
                     }
                     dispose();
                     ReadWindow rw=new ReadWindow("READ WINDOW",tname);
                     rw.show();
                     rw.setSize(800,600);
              }
              else if(s.equals("CANCEL"))
              {
                     dispose();
                     Options o=new Options("OPTIONS WINDOW");
                     o.show();
                     o.setSize(800,600);
              }
       }     
}

class Employee
{
       public static void main(String args[])
       {
              Login l=new Login("Login Window");
              l.show();
              l.setSize(800,600);
       }}

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి