Custom Search

Wednesday 23 October 2013

Create an application in GUI that executes select statement and list all records from DB. Also Update and Delete records according to Student ID. Enter query in text field, create button for executing that query and display result in text field.

Aim: Create an application in GUI that executes select statement and list all records from DB. Also Update and Delete records according to Student ID. Enter query in text field, create button for executing that     query and display result in text field.

File Name:Tabedpan.java
       package tabedpan;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager.*;
public class Tabedpan extends JFrame{
private class addhandler implements ActionListener {
 public addhandler() {  
        }
@Override
        public void actionPerformed(ActionEvent e)
        {   try {
                System.out.print("added!!");
   String query ="insert into `student`(`Name`,`ID`,`Phone`) values ( '"+tf2.getText()+"',NULL,'"+   tf4.getText()+"')";
                Statement stmt =(Statement) cn.createStatement();
                stmt.execute(query);
            } catch (SQLException ex) {
                Logger.getLogger(Tabedpan.class.getName()).log(Level.SEVERE, null, ex);
           }
   }
}
private class deletehandler implements ActionListene{
       public deletehandler() {
        }
       @Override
        public void actionPerformed(ActionEvent e) {
            try {
                System.out.print("deleted!!");
                String query ="delete from `student` where `ID`='"+tf5.getText()+"';";
                   PreparedStatement pstmt = (PreparedStatement) cn.prepareStatement(query);
                   pstmt.execute(query);
            } catch (SQLException ex) {
                Logger.getLogger(Tabedpan.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
    }
private class updatehandler implements ActionListener {
        private java.sql.PreparedStatement pstmt;
           private updatehandler() {
          }
@Override
        public void actionPerformed(ActionEvent e) {
            try {
               System.out.print("updated1!!");
       String query ="update `student` set `Name`='"+tf12.getText()+"',`ID`='"+tf13.getText()+"',`Phone`='"+tf14.getText()+"' where `ID`='"+tf13.getText()+"';";
               PreparedStatement pstmt = (PreparedStatement) cn.prepareStatement(query);
               pstmt.execute(query);
               System.out.print(tf12.getText());
               System.out.print(tf14.getText());
            } catch (SQLException ex) {
                Logger.getLogger(Tabedpan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
 }
    JPanel p1;
    JPanel p2;
    JPanel p3;
    JPanel p4;
     JTextField tf6;
    JTabbedPane tp;
    Connection cn;
    JTextArea ta;
    public Tabedpan() {
        try {
             this.setName("Tabedpane");
             this.setSize(800, 700);
             p1=new JPanel();
            p2=new JPanel();
            p3=new JPanel();
            p4=new JPanel();
            p10=new JPanel();
            p11=new JPanel();
            p12=new JPanel();
            p13=new JPanel();
            p5=new JPanel();
            p6=new JPanel();
            p7=new JPanel();
            p8=new JPanel();
            p9=new JPanel();
            l1=new JLabel("Enter Name");
            l2=new JLabel("Enter ID");
            l3=new JLabel("Enter Phn no");
            l11=new JLabel("Enter Name");
            l12=new JLabel("Enter ID");
            l13=new JLabel("Enter Phn no");
            b1=new JButton("Select");
            b2=new JButton("Update");
            b3=new JButton("Delete");
            b4=new JButton("Add");
            b5=new JButton("Execute");
            tp=new JTabbedPane();
            tf1=new JTextField("Enter select query");
          // tf6=new JTextField("Enter any query");
            tf2=new JTextField("Enter name");
            tf3=new JTextField("Enter id");
            tf4=new JTextField("Enter phn no");
            tf12=new JTextField("Enter name");
            tf13=new JTextField("Enter id");
            tf14=new JTextField("Enter phn no");
            tf5=new JTextField("Enter name");
            ta= new  JTextArea(10, 10);
            p1.add(ta);
            tp.addTab("Select",p1);
            tp.addTab("Update",p6);
            tp.addTab("Delete",p3);
            tp.addTab("Add",p10);
            this.add(tp);
            Class.forName("com.mysql.jdbc.Driver");
            cn = (Connection) DriverManager.getConnection("jdbc:MySql://localhost:3306/student", "root", "root");
            System.out.println("Connection Established");
            p1.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10));      
            p1.add(tf1);      
            p1.add("Select",b1);
            b1.addActionListener(new Submithandler());
            b2.addActionListener(new updatehandler());
            b3.addActionListener(new deletehandler());
            b4.addActionListener(new addhandler());
            p6.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10));
            p2.setLayout(new GridLayout(3,1));
            p7.setLayout(new FlowLayout(FlowLayout.LEFT, 55, 10));
            p8.setLayout(new FlowLayout(FlowLayout.LEFT, 75, 10));
            p9.setLayout(new FlowLayout(FlowLayout.LEFT, 50, 10));
            p2.add(p7);
            p2.add(p8);
            p2.add(p9);
            p7.add(l11);
            p8.add(l12);
            p9.add(l13);
            p6.add(p2);
            p7.add(tf12);
            p8.add(tf13);
            p9.add(tf14);
            p6.add("Update",b2);
            p3.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10));
            p3.add(tf5);
            p3.add("Delete",b3);        
            p10.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10));
            p4.setLayout(new GridLayout(3,1));
            p11.setLayout(new FlowLayout(FlowLayout.LEFT, 55, 10));
            p12.setLayout(new FlowLayout(FlowLayout.LEFT, 75, 10));
            p13.setLayout(new FlowLayout(FlowLayout.LEFT, 50, 10));
            p4.add(p11);
            p4.add(p12);
            p4.add(p13);
            p11.add(l1);
            p12.add(l2);
            p13.add(l3);
            p10.add(p4);
            p11.add(tf2);
            p12.add(tf3);
            p13.add(tf4);
            p10.add("Add",b4);
            p5.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10));
//            p5.add(tf6);
            p5.add("Execute",b5);
            this.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(Tabedpan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Tabedpan.class.getName()).log(Level.SEVERE, null, ex);
         }
}
public class Submithandler implements ActionListener{
 @Override
        public void actionPerformed(ActionEvent e) {
            try {
                String query =tf1.getText();
                Statement stmt = (Statement) cn.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                     while (rs.next()) {
                    String s=ta.getText();
                    String s1=rs.getString(1) + "\n"+rs.getInt(2) +"\n"+ rs.getInt(3)+ "\n"+ "\n";
                    ta.setText(s + s1 +" \n");
                    System.out.print("Name   " + rs.getString("Name") + "\n");
                    System.out.print("Id  " + rs.getInt(2) + "\n");
                    System.out.print("phn No   " + rs.getInt(3) + "\n \n");
                  }
              } catch (SQLException ex) {
                Logger.getLogger(Tabedpan.class.getName()).log(Level.SEVERE, null, ex);
                }
           }
      }
      public static void main(String[] args) {
        Tabedpan td=new Tabedpan();
         }
       }

No comments:

Post a Comment

Laptops