Cari Blog Ini

Senin, 05 Desember 2016

Aplikasi Login Java Swing dengan Mysql



Berikut Kodingan nya :




import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.awt.Desktop;
import java.net.URI;

class Aplikasi_Login extends JFrame
{
//JLabel wall=new JLabel(new ImageIcon("gambar/bg_login.jpg"));
JPanel panel = new JPanel ();

JLabel judul = new JLabel ("APLIKASI LOGIN");
JLabel garis = new JLabel ("==========================================");

JLabel text = new JLabel ("Mau Login Ke Browser Silahkan Klik Link Di Bawah ini");

JLabel help = new JLabel ("Help");
JLabel lapor = new JLabel ("Jika Bermasalah Silahkan Lapor Ke Admin Terima Kasih");

  JLabel username=new JLabel("User Name");
  JTextField txusername=new JTextField(20);

  JLabel passwd=new JLabel("Password");
  JPasswordField txpasswd=new JPasswordField(20) ; //--->textfield buaat password

  JButton login=new JButton(new ImageIcon ("gambar/login.jpg"));
  JButton reset=new JButton(new ImageIcon ("gambar/reset.jpg"));
  JButton exit=new JButton(new ImageIcon ("gambar/exit.jpg"));
  JButton face=new JButton(new ImageIcon ("gambar/face.jpg"));
  JButton twit=new JButton(new ImageIcon ("gambar/twit.jpg"));

   String nama="";
   String password="";


  Aplikasi_Login()
  {
    setTitle("Aplikasi LOGIN");
    setLocation(0,0);
    setSize(400, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

  void komponenvisual()
  {
    getContentPane().setLayout(null);

    getContentPane().add(judul);
    judul.setBounds(130,20,150,30);
    judul.setFont(new Font("Comic Sans MS",Font.BOLD,14));
    judul.setForeground(Color.white);

    getContentPane().add(garis);
    garis.setBounds(40,50,350,20);
    garis.setForeground(Color.white);

    getContentPane().add(username);
    username.setBounds(15,90,80,20);
    username.setForeground(Color.white);

    getContentPane().add(txusername);
    txusername.setBounds(100,90,200,50);
    txusername.setForeground(new Color(204, 204, 204));
    txusername.setFont(new Font("DejaVu Sans", Font.ITALIC, 12));
    txusername.setText("Masukkan Username");

    getContentPane().add(passwd);
    passwd.setBounds(15,150,80,20);
    passwd.setForeground(Color.white);

    getContentPane().add(txpasswd);
    txpasswd.setBounds(100,150,200,50);
    txpasswd.setForeground(new Color(204, 204, 204));
    txpasswd.setFont(new Font("DejaVu Sans", Font.ITALIC, 12));
    txpasswd.setText("Masukkan Password");
    txpasswd.setEchoChar('*');  //--->untuk mengubah karakter password

    getContentPane().add(login);
    login.setBounds(40,210,80,50);
    login.setCursor(new Cursor(Cursor.HAND_CURSOR));

    getContentPane().add(reset);
    reset.setBounds(130,210,80,50);

    getContentPane().add(exit);
    exit.setBounds(220,210,80,50);

    getContentPane().add(text);
    text.setBounds(20,270,400,20);
    text.setForeground(Color.white);

    getContentPane().add(face);
    face.setBounds(20,310,150,50);
    face.setCursor(new Cursor(Cursor.HAND_CURSOR));  //untuk mengganti cursor tangan
    face.setToolTipText("Facebook"); //untuk membuat comentar


    getContentPane().add(twit);
    twit.setBounds(190,310,150,50);
    twit.setCursor(new Cursor(Cursor.HAND_CURSOR));  //untuk mengganti cursor tangan
    twit.setToolTipText("Twitter"); //untuk membuat comentar

    getContentPane().add(help);
    help.setBounds(20,370,100,20);
    help.setCursor(new Cursor(Cursor.HAND_CURSOR));
    help.setForeground(Color.white);

    getContentPane().add(lapor);
    lapor.setBounds(20,400,400,20);
    lapor.setVisible(false);
  //lapor.setForeground(Color.black);

    getContentPane().add(panel);
    panel.setBounds(10,10,360,440);
    panel.setBorder(new BevelBorder(BevelBorder.RAISED));
    panel.setBackground(Color.BLACK);

    setVisible(true);
  }

 void AksiReaksi()
 {
 txusername.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                txusername.setText("");
                txusername.setForeground(Color.BLACK);
                txusername.setFont(new Font("DejaVu Sans", Font.PLAIN, 12));
            } });

txpasswd.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                txpasswd.setText("");
                txpasswd.setForeground(Color.BLACK);
                txpasswd.setFont(new Font("DejaVu Sans", Font.PLAIN, 12));
            } });

//========================== tombol login =====================//
  login.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    if(e.getSource()==login)
    {
     try
     {
      nama=txusername.getText();
      password=new String(txpasswd.getPassword());

Class.forName("com.mysql.jdbc.Driver");
Connection koneksi = DriverManager.getConnection("jdbc:mysql://localhost/DataMahasiswa","root","root");
Statement stm=koneksi.createStatement();
String sql ="select * from datalogin where Username='"+nama+"'and password='"+password+"'";
ResultSet rs=stm.executeQuery(sql);
 if(rs.next())
          {
 if((rs.getString(1).equals(nama))&&(rs.getString(2).equals(password)))
JOptionPane.showMessageDialog(null,"Login berhasil","Konfirmasi",JOptionPane.INFORMATION_MESSAGE);

 Aplikasi_Penilaian_Mhs ap=new Aplikasi_Penilaian_Mhs();
  ap.komponenVisual();
  ap.AksiReaksi();
dispose();    //untuk menutup login
          }
          else
          {
JOptionPane.showMessageDialog(null,"Login salah","Konfirmasi",JOptionPane.INFORMATION_MESSAGE);
          }
          stm.close();
          koneksi.close();
    }
     catch(Exception ex)
     {
      System.out.println("Error: "+ex);
 }  }  }  } );

//========================== tombol reset =====================//

 reset.addActionListener(new ActionListener()
 {
  public void actionPerformed(ActionEvent e)
  {
   txusername.setText("");
   txpasswd.setText("");
  }  } );

//========================== tombol exit =====================//

  exit.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
      System.exit(0);
      }  }  );
//========================== tombol facebook =====================//
face.addActionListener(new ActionListener()    //untuk tombol facebook
  {
   public void actionPerformed(ActionEvent e)
   {
try
    {
    Desktop.getDesktop().browse(new URI("http://www.facebook.com/ramadhani"));
    }
    catch(Exception ex)
    {
    System.out.println(ex);
}  }  }   );

//========================== tombol twitter =====================//

twit.addActionListener(new ActionListener()    //untuk tombol twiter
  {
   public void actionPerformed(ActionEvent e)
   {
try
{
Desktop.getDesktop().browse(new URI("http://www.twitter.com"));
}
catch(Exception ex)
{
System.out.println(ex);
}  }  }   );

//========================== help =====================//

help.addMouseListener(new MouseAdapter()     // untuk text help
{
public void mouseClicked(MouseEvent e)
 {
if (e.getSource()==help)
 {
  lapor.setVisible(true);
  lapor.setForeground(Color.white);
    }  }  }  );
 } //tutup aksi reaksi

  public static void main (String args[])
  {
  Aplikasi_Login aL=new Aplikasi_Login();
  aL.komponenvisual();
  aL.AksiReaksi();
  }  }