Total Tayangan Halaman

Rabu, 06 Mei 2015

MEMBUAT MENU LOGIN DENGAN JAVA

Salam,
sahabat setia, kita berjumpa kembali di tutorial java. kali saya akan membahas mengai cara membuat  login menggunakan  java dan mysql.  oke sahabat simak tutorial selengkapnya :

form login merupakan form yang diharuskan ada dalam suatu paket aplikasi sistem, karena untuk menjaga keamanan dan membatasi penggunaan sistem. misalkan ada sebuah rumah, dalam rumah tersebut form login merupakan pintu untuk memasuki rumah tersebut.
dalam Tutorial Lanjutan Java : Membuat Login Pada Java dan mysql ini saya akan membahas cara membuat dan mengkoneksikan form login dengan mysql sampai tuntas.
oke, langsung ke cara membuat  login menggunakan  java dan mysql :
  • buat paket aplikasi baru (dengan nama terserah pada sahabat)
  • buat juga sebuah JFrame From (dengan nama login)
  • dan sebuah class java (koneksi), kelas koneksi ini digunakan untuk menghubungkan aplikasi dengan database.
  • jangan lupa buat databasenya 
persiapan siap, langkah selanjutnya ke code program :
package Form38;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;


/**
 *
 * @author hasan
 */
public class Form_Login extends javax.swing.JFrame {
Connection con;
    Statement stt;
    ResultSet rss;
   
    /**
     * Creates new form Form_Login
     */
    public Form_Login() {
       
        initComponents();
        this.getRootPane().setDefaultButton(login);
        Koneksi();
      setLocationRelativeTo(this);
    }

    public void Koneksi(){
    try{
        Class.forName("com.mysql.jdbc.Driver");//1. Load Driver
        String database = "jdbc:mysql://localhost:3306/rm38db";//2. Memanggil alamat database
        String userDtbs = "root";//username default database
        String passDtbs = "hasan";//password default database
        con = DriverManager.getConnection(database,userDtbs,passDtbs);//3. Membuat Koneksi Database
        stt = con.createStatement();//membuat statement
        }
        catch(Exception e){
        JOptionPane.showMessageDialog(null,"Gagal Koneksi","Pesan",JOptionPane.ERROR_MESSAGE);
        }
    }
   
   

        private void txtusernameActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {                                    
System.exit(0);        // TODO add your handling code here:
    }                                   

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {                                     
     try{
    String sql = "SELECT * FROM login WHERE nik = '"+ txtusername.getText()+"' and password = '"+String.valueOf(txtpassword.getPassword())+"'";
    rss = stt.executeQuery(sql);
    if(rss.next()){   
        String nik = rss.getString(1);
        String password = rss.getString(2);
        new MenuUtama().show();
         this.dispose();
       
    }else{
        reportlabel.setText("Harap Periksa nik & Password atau hub admin...!");
        txtusername.setText("");
        txtpassword.setText("");
    }
  }catch(Exception e){
      e.printStackTrace();
  }
                      
                                          
                                             

                   // TODO add your handling code here:
    }                                    

    private void txtusernameKeyPressed(java.awt.event.KeyEvent evt) {                                      
        // TODO add your handling code here:
    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form_Login().setVisible(true);
            }
        });
    }
Agar tampilan Form menjadi kelihatan menarik buat class panel untuk kita gunakan agar tampilan lebih menarik berikut kodenya:
class panel.java
package komponen;
     import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.geom.GeneralPath;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;

    /**
     *
     * @author hasan     */
    public class Panel extends JPanel {

        private static final long serialVersionUID = -1;
        private BufferedImage gradientImage;
        private Color blue = Color.BLUE;
        private Color cyan = Color.CYAN;

        /**
         * membuat panel white cyan
         */
        public Panel() {
            super();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (isOpaque()) {
                Graphics2D g2 = (Graphics2D) g.create();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                setUpGradient();
                g2.drawImage(gradientImage, 0, 0, getWidth(), getHeight(), null);

                int width = getWidth();
                int height = getHeight() * 5 / 100;

                Color light = new Color(1F, 1F, 1F, 0.5F);
                Color dark = new Color(1F, 1F, 1F, 0.0F);

                GradientPaint paint = new GradientPaint(0, 0, light, 0, height, dark);
                GeneralPath path = new GeneralPath();
                path.moveTo(0, 0);
                path.lineTo(0, height);
                path.curveTo(0, height, width / 2, height / 2, width, height);
                path.lineTo(width, 0);
                path.closePath();

                g2.setPaint(paint);
                g2.fill(path);

                paint = new GradientPaint(0, getHeight(), light, 0, getHeight() - height, dark);
                path = new GeneralPath();
                path.moveTo(0, getHeight());
                path.lineTo(0, getHeight() - height);
                path.curveTo(0, getHeight() - height, width / 2, getHeight() - height / 2, width, getHeight() - height);
                path.lineTo(width, getHeight());
                path.closePath();

                g2.setPaint(paint);
                g2.fill(path);
                g2.dispose();
            }
        }

        /**
         * membuat gambar background gradient
         */
        private void setUpGradient() {
            gradientImage = new BufferedImage(1, getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = (Graphics2D) gradientImage.getGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            GradientPaint paint = new GradientPaint(0, 0, blue, 0, getHeight(), cyan);

            g2.setPaint(paint);
            g2.fillRect(0, 0, 1, getHeight());
            g2.dispose();
        }
    }


Class PanelBackGround:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package komponen;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author hasan */
public class PanelBackGroundForm extends JPanel {
     private Image image;

    public PanelBackGroundForm() {
        image = new ImageIcon(getClass().getResource("/komponen/10.jpg")).getImage();
    }

    @Override
    protected void paintComponent(Graphics grphcs) {
        super.paintComponent(grphcs);

        Graphics2D gd = (Graphics2D) grphcs.create();
        gd.drawImage(image, 0, 0, getWidth(), getHeight(), null);
        gd.dispose();
    }

}
Class PanelDao:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package komponen;


    /**
     *
     * @author Jie
     */
    public interface PanelDao {
        public static final int BACKGROUND_IMAGE_CENTER_BOTTOM = 0;
        public static final int BACKGROUND_IMAGE_CENTER_MIDDLE = 1;
        public static final int BACKGROUND_IMAGE_CENTER_TOP = 2;
        public static final int BACKGROUND_IMAGE_LEFT_BOTTOM = 3;
        public static final int BACKGROUND_IMAGE_LEFT_MIDDLE = 4;
        public static final int BACKGROUND_IMAGE_LEFT_TOP = 5;
        public static final int BACKGROUND_IMAGE_RIGHT_BOTTOM = 6;
        public static final int BACKGROUND_IMAGE_RIGHT_MIDDLE = 7;
        public static final int BACKGROUND_IMAGE_RIGHT_TOP = 8;
        public static final int BACKGROUND_IMAGE_STRECT = 9;
        public static final int BACKGROUND_IMAGE_TILED = 10;
    }

Drag n Drop Class Panel Ke Dalam Form Login yang sudah kita buat :
Contoh


Tidak ada komentar:

Posting Komentar