Adv Java - [JDBC: Example on Applet]

♠ Posted by Unknown in at 02:00

JDBC Connection on Applet

This program is related to JDBC Connectivity on Applet. Program is written into directly Notepad Editor. So, Designing of program is basically shows how to use Layout Manager properly to fit the controls on desired place.


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

public class JDBCApp extends JApplet implements ActionListener
{
          JLabel lEno, lEname, lEdesig, lEsal, lEcity;
          JTextField tEno, tEname, tEdesig, tEsal, tEcity;
          JButton bFirst, bPrv, bNext, bLast, bAdd, bSave, bDelete, bView;
         
          String dbUrl = "jdbc:odbc:Employee";
          String user = "";
          String password = "";
          Connection c;
          Statement s;
          ResultSet r;
         
          public void init()
          {
                   try
                   {
                             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                             c = DriverManager.getConnection(dbUrl, user, password);
                             s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
                             r = s.executeQuery("Select * From Emp_Mst");
                   }catch(Exception e){}
                  
                   JPanel jp = new JPanel(new GridLayout(5,2));
                   JPanel jpButton = new JPanel(new GridLayout(1,4));
                   JPanel jpCommand = new JPanel(new GridLayout(1,3));
                  
                   lEno = new JLabel("Enter Employee No. : ");
                   lEname = new JLabel("Enter Employee Name : ");
                   lEdesig = new JLabel("Enter Employee Designation : ");
                   lEsal = new JLabel("Enter Employee Salary : ");
                   lEcity = new JLabel("Enter Employee City : ");
                  
                   tEno = new JTextField(3);
                   tEname = new JTextField(3);
                   tEdesig = new JTextField(3);
                   tEsal = new JTextField(3);
                   tEcity = new JTextField(3);
                  
                   bFirst = new JButton("First");
                   bPrv = new JButton("Previous");
                   bNext = new JButton("Next");
                   bLast = new JButton("Last");
                   bAdd = new JButton("Add");
                    bSave = new JButton("Save");
                   bDelete = new JButton("Delete");
                   bView = new JButton("View");
                  
                   jp.add(lEno);
                   jp.add(tEno);
                   jp.add(lEname);
Image: JDBC Form Entry                   jp.add(tEname);
                   jp.add(lEdesig);
                   jp.add(tEdesig);
                   jp.add(lEsal);
                   jp.add(tEsal);
                   jp.add(lEcity);
                   jp.add(tEcity);
                  
                   jpButton.add(bFirst);
                   jpButton.add(bPrv);
                   jpButton.add(bNext);
                   jpButton.add(bLast);
                  
                   jpCommand.add(bView);
                   jpCommand.add(bAdd);
                   jpCommand.add(bSave);
                   jpCommand.add(bDelete);
                  
                   Container contentPane = getContentPane();
                   contentPane.add(jp,BorderLayout.NORTH);
                   contentPane.add(jpButton,BorderLayout.SOUTH);
                   contentPane.add(jpCommand, BorderLayout.CENTER);
                  
                   bFirst.addActionListener(this);
                   bPrv.addActionListener(this);
                   bNext.addActionListener(this);
                   bLast.addActionListener(this);
                  
                   bAdd.addActionListener(this);
                   bSave.addActionListener(this);
                   bDelete.addActionListener(this);
                   bView.addActionListener(this);
                  
          }
         
          public void actionPerformed(ActionEvent ae)
          {
                   String head = ae.getActionCommand();
                   try
                   {
                             if(head == "First")
                             {
                                      r.first();
                                      tEno.setText(r.getString("EmpId"));
                                      tEname.setText(r.getString("EmpName"));
                                      tEdesig.setText(r.getString("Emp_Desig"));
                                      tEsal.setText(r.getString("EmpSal"));
                                      tEcity.setText(r.getString("EmpCity"));
                             }
                             else if(head == "Previous")
                             {
                                      r.previous();
                                      if(r.isBeforeFirst())
                                      {
                                                r.first();
                                      }
                                      tEno.setText(r.getString("EmpId"));
                                      tEname.setText(r.getString("EmpName"));
                                      tEdesig.setText(r.getString("Emp_Desig"));
                                      tEsal.setText(r.getString("EmpSal"));
                                      tEcity.setText(r.getString("EmpCity"));
                             }
                             else if(head == "Next")
                             {
                                      r.next();
                                      if(r.isAfterLast())
                                      {
                                                r.last();
                                      }
                                      tEno.setText(r.getString("EmpId"));
                                      tEname.setText(r.getString("EmpName"));
                                      tEdesig.setText(r.getString("Emp_Desig"));
                                      tEsal.setText(r.getString("EmpSal"));
                                      tEcity.setText(r.getString("EmpCity"));
                             }
                             else if(head == "Last")
                             {
                                      r.last();
                                      tEno.setText(r.getString("EmpId"));
                                      tEname.setText(r.getString("EmpName"));
                                      tEdesig.setText(r.getString("Emp_Desig"));
                                      tEsal.setText(r.getString("EmpSal"));
                                      tEcity.setText(r.getString("EmpCity"));
                             }
                             else if(head == "Add")
                             {
                                      tEno.setText("");
                                      tEname.setText("");
                                      tEdesig.setText("");
                                      tEsal.setText("");
                                      tEcity.setText("");
                             }
                             else if(head == "View")
                             {
                                      r = s.executeQuery("Select * From Emp_Mst");
                                      r.first();
                                      tEno.setText(r.getString("EmpId"));
                                      tEname.setText(r.getString("EmpName"));
                                      tEdesig.setText(r.getString("Emp_Desig"));
                                      tEsal.setText(r.getString("EmpSal"));
                                      tEcity.setText(r.getString("EmpCity"));
                             }
                             else if(head == "Save")
                             {
                                      int Eno = Integer.parseInt(tEno.getText());
                                      String Ename = tEname.getText();
                                      String Edesig = tEdesig.getText();
                                      int Esal = Integer.parseInt(tEsal.getText());
                                      String Ecity = tEcity.getText();
                                     
                                      String InsertEmp = "Insert into Emp_Mst Values(" + Eno + ",'" + Ename + "','" + Edesig + "'," + Esal + ",'" + Ecity + "')";
                                      s.executeUpdate(InsertEmp);
                             }
                             else
                             {
                                      int Eno = Integer.parseInt(tEno.getText());
                                      String DelEmp = "Delete From Emp_Mst Where Empid = " + Eno;
                                      s.executeUpdate(DelEmp);
                                      r = s.executeQuery("Select * From Emp_Mst");
                                      r.first();
                                      tEno.setText(r.getString("EmpId"));
                                      tEname.setText(r.getString("EmpName"));
                                      tEdesig.setText(r.getString("Emp_Desig"));
                                      tEsal.setText(r.getString("EmpSal"));
                                      tEcity.setText(r.getString("EmpCity"));
                                     
                             }
                   }catch(Exception e){tEno.setText(e.toString());}
          }
         

}

0 comments:

Post a Comment