Home » , » Java Script

Java Script

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

class db1 extends JFrame implements ActionListener
{ private Container contain1=new Container();
  private JPanel jpanel1=new JPanel();
  private JPanel jpanel2=new JPanel();
  private JPanel jpanel3=new JPanel();
  private JPanel jpanel4=new JPanel();

  private JLabel label1=new JLabel("NIM            :");
  private JLabel label2=new JLabel("Nama Mahasiswa :");
  private JLabel label3=new JLabel("Alamat :");
  private JLabel label4=new JLabel("Hobby :");
  private JLabel label5=new JLabel("Jenis Kelamin :");
  private JLabel label6=new JLabel("Sistem Operasi Favorit :");

  private JTextField text1= new JTextField(8);
  private JTextField text2= new JTextField();
  private JTextField text3= new JTextField();
  private JButton buton1=new JButton("Save");
  private JButton buton2=new JButton("Quit ");
  private JButton buton3=new JButton("Report ");
  private JButton buton4=new JButton("Batal ");

  private Checkbox baca=new Checkbox("Membaca buku",true);
  private Checkbox jalan=new Checkbox("Jalan-Jalan");
  private Checkbox makan=new Checkbox("Makan");

  private CheckboxGroup g=new CheckboxGroup();
  private Checkbox pria=new Checkbox("Pria",g,true);
  private Checkbox wanita=new Checkbox("Wanita",g,false);
  private Choice os = new Choice();
  String u2; //untuk simpan pada modus edit
  Object[] options = {"Input","Edit","Delete"};
  int pilih; 
 
  db1()
  {super("Data Mahasiswa FTI Budi Luhur");
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buton1.addActionListener(this);
    buton2.addActionListener(this);
    text1.addActionListener(this);
    text2.addActionListener(this);
    text3.addActionListener(this);
    buton3.addActionListener(this);
    buton4.addActionListener(this);

    buton1.setEnabled(false);
        buton1.setMnemonic(KeyEvent.VK_S);
        buton2.setMnemonic(KeyEvent.VK_Q);
        buton3.setMnemonic(KeyEvent.VK_R);
    text2.setEnabled(false);
    text3.setEnabled(false);
      
    os.setEnabled(false);

   setSize(500,300);
   setLocation(100,100);
   setResizable(false);
   jpanel1.setLayout(new GridLayout(4,2));
   jpanel1.add(label1);
   jpanel1.add(text1);
   jpanel1.add(label2);
   jpanel1.add(text2);
   jpanel1.add(label3);
   jpanel1.add(text3);

   jpanel2.setLayout(new GridLayout(2,4));
   jpanel2.add(label4); //hoby
     jpanel2.add(baca);
     jpanel2.add(jalan);
     jpanel2.add(makan);
   jpanel2.add(label5); //jenis kelamin
     jpanel2.add(pria);
     jpanel2.add(wanita);

   jpanel3.setLayout(new GridLayout(3,2));
   jpanel3.add(label6); //so favorit
  os.addItem("Unix");
  os.addItem("Microsoft Windows");
  os.addItem("Linux");
   jpanel3.add(os);
   jpanel3.add(buton1);
   jpanel3.add(buton2);
   jpanel3.add(buton3);
   jpanel3.add(buton4);

   jpanel4.setLayout(new BorderLayout());
   jpanel4.add(jpanel1,BorderLayout.NORTH);
   jpanel4.add(jpanel2,BorderLayout.CENTER);
   jpanel4.add(jpanel3,BorderLayout.SOUTH);

   contain1=getContentPane();
   contain1.add(jpanel4);
   
   InEdDeL();
   show();
  } //end constructor sabun1 
  
 public void actionPerformed(ActionEvent e)
  { int n=99;
    String a1="",a2="",a3="",tt="",a4="1";

   Object ojek=e.getSource();
    if(ojek==text1)
     { text2.setEnabled(true);
       if(pilih==1) //edit 
          {text1.setEnabled(false);
           caridata();
          }
       if (pilih==2) //delete 
          {text2.setEnabled(false);
           caridata();
          }
       text2.requestFocus();
     }
    else if(ojek==text2) 
     { text3.setEnabled(true);
       os.setEnabled(true);
       text3.requestFocus();
 buton1.setEnabled(true);
     }
    else if(ojek==buton3) 
     { laporan();
     }
    else if(ojek==buton4) 
     {  bersih();
        
        InEdDeL();
     }
    else if(ojek==buton2)
      { JOptionPane.showMessageDialog(null,"See you !!");
      System.exit(0);}
    else if(ojek==buton1) 
         { 
           String f1,f2,f3;
           f1=text1.getText().trim();
    f2=text2.getText().trim();
    f3=text3.getText().trim();
           if (f1.length()<1 || f2.length()<1 ||f3.length()<1)
              { JOptionPane.showMessageDialog(null,"Data belum lengkap !");
         requestFocus();
              }
           else
              {   //simpan ke file
         if (baca.getState()==true)
            {a2="Baca Buku   ";
             tt+="1";} 
         if (jalan.getState()==true)
            {a2+="Jalan-Jalan    ";
             tt+="2";} 
          if (makan.getState()==true)
            {a2+="makan-makan    ";
             tt+="3";}
        if (pria.getState()==true)
           { a3="Pria ";
             a4="1";} 
         else 
           { a3="Wanita"; 
               a4="2"; }
  
         a1="NPM : "+text1.getText()+"\n Nama : "+text2.getText()+"\n Alamat : ";
         a1+=text3.getText()+"\n Hobby : "+a2;
         a1+="\nJenis Kelamin :"+a3+"\nFavourite Operating System : "+os.getSelectedItem();
         a1+="\n\nSave Success !\n\n" ;
          
  n = JOptionPane.showConfirmDialog(null,a1+"Would you like to "+options[pilih]+" record ?",
             "Matrikulasi Information System",JOptionPane.YES_NO_OPTION); 
   try
           {
                Class.forName("com.mysql.jdbc.Driver"); //bisa
           Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost/mhsui?user=root&password=rahasia");
           Statement Stmt = Conn.createStatement();
            if(pilih==0 && n==0) //entry 
       { ResultSet RS = Stmt.executeQuery("SELECT * from mhs where npm='"+text1.getText()+"'"); //dml database
         int a=0;
         while(RS.next())
                { a++;
                }
       if (a==0) //tidak ketemu
           { Stmt.executeUpdate("insert into mhs values"+"('"+text1.getText()+"','"+text2.getText()+"','"+text3.getText()+"','"+
             tt+"','"+a4+"','"+os.getSelectedItem()+"')");
      text1.setEnabled(true);
                    JOptionPane.showMessageDialog(null,"Your Saving is Success !!");
           }
                else   
    { 
      JOptionPane.showMessageDialog(null,"Your Saving is not Success, There is another data like this !");
                  }
                RS.close();
               } 
            else if(pilih==1 && n==0) //edit 
            { String edit="update mhs set nama='"+text2.getText()+"',alamat='"+text3.getText()+"',hoby='"+tt+"',jk='"+a4+"',OS='"+
    os.getSelectedItem()+"' where npm='"+text1.getText()+"'";
                     Stmt.executeUpdate(edit);
             JOptionPane.showMessageDialog(null,"Your Edit is Success !!");
     }  
            else if(pilih==2&&n==0) //delete 
     {String del="delete from mhs where npm='"+text1.getText()+"'";
                     Stmt.executeUpdate(del);
             JOptionPane.showMessageDialog(null,"Your Delete is Success !!");
     }
  bersih(); 
     Stmt.close();
            Conn.close();
            }
 catch(java.lang.ClassNotFoundException ee) 
      { System.err.println("Unable to load driver.");
       System.err.println(ee.getMessage()); 
         }
            catch (SQLException E) 
            {  System.out.println("SQLException: " + E.getMessage());
              System.out.println("SQLState:     " + E.getSQLState());
              System.out.println("VendorError:  " + E.getErrorCode());
        }
 
 if (n==0)
       { bersih();
       }
     else
        { JOptionPane.showMessageDialog(null,"See you !!");
          System.exit(0);
         }
     }
      }
     } //akhir else

private void InEdDeL()
{ pilih = JOptionPane.showOptionDialog(this,"Proses apa yang akan anda lakukan !!",
       "Sistem Informasi FTI Budi Luhur University",JOptionPane.YES_NO_CANCEL_OPTION,
       JOptionPane.QUESTION_MESSAGE,null,options,options[2]);
 if (pilih==0)
    buton1.setText("Save"); 
 else if(pilih==1)
       buton1.setText("Edit");
 else if(pilih==2)
       buton1.setText("Delete");
}
private void bersih()
{ text1.setText(""); baca.setState(true);  pria.setState(true);
         text2.setText(""); jalan.setState(false); wanita.setState(false);
         text3.setText(""); makan.setState(false); os.select(0);
 buton1.setEnabled(false);
 text1.setEnabled(true);
 text2.setEnabled(false);
 text3.setEnabled(false);
 os.setEnabled(false);
 text1.requestFocus();
 }
private void caridata()
{  try
    { 
     Class.forName("com.mysql.jdbc.Driver"); //bisa
     Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost/mhsui?user=root&password=rahasia");
     Statement Stmt = Conn.createStatement();
         
     ResultSet RS = Stmt.executeQuery("SELECT * from mhs where npm='"+text1.getText()+"'"); //dml database
 int a=0;
 while(RS.next())
          { a++;
            text2.setText(RS.getString("NAMA")); 
            text3.setText(RS.getString("ALAMAT")); 

        String hob=RS.getString("HOBY");
        for(int i=0;i<hob.length();i++)
               { if (hob.charAt(i)=='1')
                  baca.setState(true);
     if (hob.charAt(i)=='2')
                  jalan.setState(true);
     if (hob.charAt(i)=='3')
                   makan.setState(true);
               }
        hob=RS.getString("JK");
        if(hob.charAt(0)=='1')
          pria.setState(true);
               else
           wanita.setState(true);  
        hob=RS.getString("OS");
                if(hob.charAt(0)=='U')
            os.select(0);
               else if(hob.charAt(0)=='M')
     os.select(1);
               else 
      os.select(2);
       
               buton1.setEnabled(true);
               JOptionPane.showMessageDialog(null,"ada ");
           }

     if (a==0) 
 {JOptionPane.showMessageDialog(null,"Data tidak ada !");
         text2.setEnabled(false);
         text1.setEnabled(true);
         text1.requestFocus();
        }
     RS.close();  
     Stmt.close();
     Conn.close(); 
  
    }
 catch(java.lang.ClassNotFoundException ee) 
      { System.err.println("Unable to load driver.");
       System.err.println(ee.getMessage()); 
         }
            catch (SQLException E) 
            {  System.out.println("SQLException: " + E.getMessage());
              System.out.println("SQLState:     " + E.getSQLState());
              System.out.println("VendorError:  " + E.getErrorCode());
        }
} 
private void laporan()
{  try
    { 
     Class.forName("com.mysql.jdbc.Driver"); //bisa
     Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost/mhsui?user=root&password=rahasia");
     Statement Stmt = Conn.createStatement();
     final boolean RS = Stmt.execute("SELECT * from mhs");
           if (RS){
              // Get results of Query
              final ResultSet result = Stmt.getResultSet();
              if (result!=null) 
                displayResults(result);
             }
     Stmt.close();
     Conn.close();     
   }
 catch(java.lang.ClassNotFoundException ee) 
      { System.err.println("Unable to load driver.");
       System.err.println(ee.getMessage()); 
         }
            catch (SQLException E) 
            {  System.out.println("SQLException: " + E.getMessage());
              System.out.println("SQLState:     " + E.getSQLState());
              System.out.println("VendorError:  " + E.getErrorCode());
        }
}   
   void displayResults (final ResultSet r) throws SQLException 
    {    final ResultSetMetaData rmeta = r.getMetaData();
  
        // Use the meta data to obtain the number of columns
        final int numColumns=rmeta.getColumnCount();
        String text="";
   text+="NIM       Nama Mahasiswa      Alamat    Hobby       JK OS Favorit\n";
       text+="\n";
       int j;
       while (r.next())
        {for(int i=1;i<=numColumns;++i) 
           {final String s = r.getString(i);
             if (s==null) 
                {text += "NULL";}
             else 
               {text+=s;}
        
            if(i==2) {for(j=1;j<s.length();j++) text+=" ";}
             if(i<numColumns) text += " | ";
           }
         text+="\n";
        }
 JOptionPane.showMessageDialog(null,text);
    }  

}

class database
{public static void main(String [] args) 
 { 
   new db1();
 } 
}

 


0 komentar:

Posting Komentar

Popular Posts

Comments

Kontak Jodoh

Featured

free counters

Arsip Blog

Followers