BCA

AJAVA LAB

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6

PART B

PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10 PROGRAM 11 PROGRAM 12 PROGRAM 13 PROGRAM 14 PROGRAM 15 . . .

 
 
  /* 
 * Program12.java is a 1.4 example that requires one additional file:
 *   images/middle.gif. 
 */
import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.*;

import java.sql.*;
import java.util.*;
public class Program12 extends JPanel {


JLabel lno, lname, ldoj, ljob,lsal;
JTextField tno,tname,tdoj,tjob,tsal;
JButton insert,clear,display,update,delete;
 
    public Program12() {
        super(new GridLayout(1, 1));

        JTabbedPane tabbedPane = new JTabbedPane();
        ImageIcon icon = new ImageIcon();

        JComponent panel1 = makeTextPanel("Panel #1");
           panel1= addpanelfunction(panel1);

        tabbedPane.addTab("Add 1", icon, panel1,"Does nothing");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

        JComponent panel2 = makeTextPanel("Panel #2");
        tabbedPane.addTab("Delete 2", icon, panel2,  " ");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

        JComponent panel3 = makeTextPanel("Panel #3");
        tabbedPane.addTab("Edit 3", icon, panel3,
                          "Still does nothing");
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

        JComponent panel4 = makeTextPanel("Panel #4 ( ).");

            panel4=displayAll(panel4);
	    panel4.setPreferredSize(new Dimension(410, 50));

        tabbedPane.addTab("Display 4", icon, panel4,
                          "Does nothing at all");
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
 
        add(tabbedPane);
        
    }
        protected JComponent addpanelfunction(JComponent panel1)
            {

        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel("add");
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);

            lno=new JLabel("EMPLOYEE NO:");
            lname=new JLabel("EMPLOYEE NAME:");
            ldoj=new JLabel("DOJ:");
            lsal=new JLabel("SALARY:");
            

            tno=new JTextField(15);
            tname=new JTextField(15);
            tdoj=new JTextField(15);
            tsal=new JTextField(15);
            

            insert=new JButton("INSERT");
            clear=new JButton("CLEAR");
            panel.add(lno); panel.add(tno);
            panel.add(lname); panel.add(tname);
            panel.add(ldoj); panel.add(tdoj);
            panel.add(lsal); panel.add(tsal);
            
            panel.add(insert); panel.add(clear);

  
            clear.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent ae)
            {
                    tno.setText(null);
                    tname.setText(null);
                    tdoj.setText(null);
                    tsal.setText(null);
                    
            }
             });

insert.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
        String v1=tno.getText();
        String v2=tname.getText();
        String v3=tdoj.getText();
        int v4=Integer.parseInt(tsal.getText());
      
try
{
        Class.forName ("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytestdb","root","12345678");
            PreparedStatement st=con.prepareStatement("insert into emp(eno,ename,doj,sal) values(?,?,?,?,?)");
            st.setString(1,v1);
            st.setString(2,v2);
            st.setString(3,v3);
            st.setInt(4,v4);
            
            st.executeUpdate();
            // JOptionPane.showMessageDialog(getContentPane(),"RECORD INSERTED");
            }
            catch(Exception e)
            {
                    System.err.println(e);
                   // JOptionPane.showMessageDialog(getContentPane(),"ERROR IN INSERTING DATA!");
            }
}
});
        return panel;

            }

    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel("");
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }



  protected JComponent displayAll(JComponent panel4) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel("");
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);



        lno=new JLabel(" ENTER EMPLOYEE NO:");
        tno=new JTextField(8);
        display=new JButton("DISPLAY");
        clear=new JButton("CLEAR");
        panel.add(lno);
        panel.add(tno);
        panel.add(display);
        panel.add(clear);
        clear.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                tno.setText(null);
            }
        });
        display.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                 String v1=tno.getText();
try
      {
             Class.forName ("com.mysql.cj.jdbc.Driver");
          Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytestdb","root","12345678");
            PreparedStatement st=con.prepareStatement("SELECT eno, ename, doj, sal,job FROM emp WHERE  eno = ?");
            st.setString(1,v1);
            ResultSet rs = st.executeQuery();
        while (rs.next())
        {
                String eno=rs.getString(1);
                String ename=rs.getString(2);
                String edoj=rs.getString(3);
                int esal=rs.getInt(4);
                String ejob=rs.getString(5);
               /*  JOptionPane.showMessageDialog( this.getContentPane(),"EMPLOYEE DETAILS \n"
                +"EMPLOYEE NO : "+eno+"\n"
                +"EMPLOYEE NAME : "+ename+"\n"
                +"DATE OF JOIN : "+edoj+"\n"
                +"EMPLOYEE SALARY : "+esal+"\n"
                +"EMPLOYEE DESIGNATION : "+ejob+"\n"); */
        }
        con.close();
        }
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null,"ERROR during DISPLAY!");
}
}
});



        return panel;
    }

    

     
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated(true);

        //Create and set up the window.
        JFrame frame = new JFrame("Program12");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new Program12();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.getContentPane().add(new Program12(),
                                 BorderLayout.CENTER);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
         
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}