Advertisement
pendekar_langit

login koneksi db

Dec 3rd, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.88 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  *//*
  6.  * To change this license header, choose License Headers in Project Properties.
  7.  * To change this template file, choose Tools | Templates
  8.  * and open the template in the editor.
  9.  */
  10.  
  11. package rentalmobil.karyawan;
  12. import java.sql.*;
  13. import javax.swing.JOptionPane;
  14. /**
  15.  *
  16.  * @author surya
  17.  */
  18. public class FormLOGINKARYAWAN extends javax.swing.JFrame {
  19.     Connection con;
  20.     Statement stt;
  21.     ResultSet rss;
  22.     /**
  23.      * Creates new form FormLOGINUSER
  24.      */
  25.     public FormLOGINKARYAWAN() {
  26.         initComponents();
  27.         Koneksi();
  28.     }
  29.      public void Koneksi(){
  30.     try{
  31.         Class.forName("com.mysql.jdbc.Driver");//1. Load Driver
  32.         String database = "jdbc:mysql://localhost:3306/rental_mobil";//2. Memanggil alamat database
  33.         String userDtbs = "root";//username default database
  34.         String passDtbs = "";//password default database
  35.         con = DriverManager.getConnection(database,userDtbs,passDtbs);//3. Membuat Koneksi Database
  36.         stt = con.createStatement();//membuat statement
  37.         }
  38.         catch(Exception e){
  39.         JOptionPane.showMessageDialog(null,"Gagal Koneksi","Pesan",JOptionPane.ERROR_MESSAGE);
  40.         }
  41.     }
  42.  
  43.     /**
  44.      * This method is called from within the constructor to initialize the form.
  45.      * WARNING: Do NOT modify this code. The content of this method is always
  46.      * regenerated by the Form Editor.
  47.      */
  48.     @SuppressWarnings("unchecked")
  49.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  50.     private void initComponents() {
  51.  
  52.         jLabel2 = new javax.swing.JLabel();
  53.         jLabel3 = new javax.swing.JLabel();
  54.         inputuser = new javax.swing.JTextField();
  55.         inputpass = new javax.swing.JPasswordField();
  56.         cmdlogin = new javax.swing.JButton();
  57.         jLabel1 = new javax.swing.JLabel();
  58.  
  59.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  60.         setTitle("LOGIN USER");
  61.  
  62.         jLabel2.setText("Username");
  63.  
  64.         jLabel3.setText("Password");
  65.  
  66.         cmdlogin.setText("Login");
  67.         cmdlogin.addActionListener(new java.awt.event.ActionListener() {
  68.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  69.                 cmdloginActionPerformed(evt);
  70.             }
  71.         });
  72.  
  73.         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
  74.         jLabel1.setText("LOGIN");
  75.  
  76.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  77.         getContentPane().setLayout(layout);
  78.         layout.setHorizontalGroup(
  79.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  80.             .addGroup(layout.createSequentialGroup()
  81.                 .addContainerGap()
  82.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83.                     .addComponent(jLabel2)
  84.                     .addComponent(jLabel3))
  85.                 .addGap(18, 18, 18)
  86.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  87.                     .addComponent(jLabel1)
  88.                     .addComponent(cmdlogin)
  89.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  90.                         .addComponent(inputuser)
  91.                         .addComponent(inputpass, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
  92.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  93.         );
  94.         layout.setVerticalGroup(
  95.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  96.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  97.                 .addContainerGap()
  98.                 .addComponent(jLabel1)
  99.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
  100.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  101.                     .addComponent(jLabel2)
  102.                     .addComponent(inputuser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  103.                 .addGap(18, 18, 18)
  104.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  105.                     .addComponent(jLabel3)
  106.                     .addComponent(inputpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  107.                 .addGap(18, 18, 18)
  108.                 .addComponent(cmdlogin)
  109.                 .addContainerGap())
  110.         );
  111.  
  112.         pack();
  113.     }// </editor-fold>                        
  114.  
  115.     private void cmdloginActionPerformed(java.awt.event.ActionEvent evt) {                                        
  116.         // TODO add your handling code here:
  117.         try{
  118.     String sql = "SELECT * FROM tbuser WHERE username = '"+inputuser.getText()+"' and password = '"+String.valueOf(inputpass.getPassword())+"'";
  119.     rss = stt.executeQuery(sql);
  120.     if(rss.next()){    
  121.         String user = rss.getString(1);
  122.         String password = rss.getString(2);
  123.         JOptionPane.showMessageDialog(null, "Berhasil Login");
  124.         new MainMENUKARYAWAN().show();
  125.         this.dispose();
  126.     }else{
  127.         JOptionPane.showMessageDialog(null, "Gagal Login","Pesan",JOptionPane.ERROR_MESSAGE);
  128.     }
  129.   }catch(Exception e){
  130.       e.printStackTrace();
  131.   }
  132.                  
  133.     }                                        
  134.        
  135.     /**
  136.      * @param args the command line arguments
  137.      */
  138.     public static void main(String args[]) {
  139.         /* Set the Nimbus look and feel */
  140.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  141.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  142.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  143.          */
  144.         try {
  145.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  146.                 if ("Nimbus".equals(info.getName())) {
  147.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  148.                     break;
  149.                 }
  150.             }
  151.         } catch (ClassNotFoundException ex) {
  152.             java.util.logging.Logger.getLogger(FormLOGINKARYAWAN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  153.         } catch (InstantiationException ex) {
  154.             java.util.logging.Logger.getLogger(FormLOGINKARYAWAN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  155.         } catch (IllegalAccessException ex) {
  156.             java.util.logging.Logger.getLogger(FormLOGINKARYAWAN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  157.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  158.             java.util.logging.Logger.getLogger(FormLOGINKARYAWAN.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  159.         }
  160.         //</editor-fold>
  161.  
  162.         /* Create and display the form */
  163.         java.awt.EventQueue.invokeLater(new Runnable() {
  164.             public void run() {
  165.                 new FormLOGINKARYAWAN().setVisible(true);
  166.             }
  167.         });
  168.     }
  169.  
  170.     // Variables declaration - do not modify                    
  171.     private javax.swing.JButton cmdlogin;
  172.     private javax.swing.JPasswordField inputpass;
  173.     private javax.swing.JTextField inputuser;
  174.     private javax.swing.JLabel jLabel1;
  175.     private javax.swing.JLabel jLabel2;
  176.     private javax.swing.JLabel jLabel3;
  177.     // End of variables declaration                  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement