Advertisement
pendekar_langit

Formkoneksi

Nov 22nd, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.32 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.  
  7. package rentalmobil.user;
  8. import java.awt.HeadlessException;
  9. import java.sql.Connection;
  10. import java.sql.Driver;
  11. import java.sql.DriverManager;
  12. import java.sql.SQLException;
  13. import java.sql.ResultSet;
  14. import java.sql.Statement;
  15. import java.sql.PreparedStatement;
  16. import javax.swing.JOptionPane;
  17. /**
  18.  *
  19.  * @author surya
  20.  */
  21. public class Formkoneksi extends javax.swing.JFrame {
  22. private static Connection conn;
  23.     /**
  24.      * Creates new form Formkoneksi
  25.      */
  26.     public Formkoneksi() {
  27.         initComponents();
  28.         inputserver.setText("localhost/rental_mobil");
  29.         inputuserserv.setText("root");
  30.         inputpassserv.setText("");
  31.         inputserver.requestFocus();
  32.     }
  33.  
  34.     /**
  35.      * This method is called from within the constructor to initialize the form.
  36.      * WARNING: Do NOT modify this code. The content of this method is always
  37.      * regenerated by the Form Editor.
  38.      */
  39.     @SuppressWarnings("unchecked")
  40.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  41.     private void initComponents() {
  42.  
  43.         inputserver = new javax.swing.JTextField();
  44.         inputuserserv = new javax.swing.JTextField();
  45.         inputpassserv = new javax.swing.JTextField();
  46.         jLabel1 = new javax.swing.JLabel();
  47.         jButton1 = new javax.swing.JButton();
  48.         jLabel4 = new javax.swing.JLabel();
  49.         jButton2 = new javax.swing.JButton();
  50.         jLabel5 = new javax.swing.JLabel();
  51.  
  52.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  53.  
  54.         jLabel1.setText("Server");
  55.  
  56.         jButton1.setText("Conect");
  57.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  58.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  59.                 jButton1ActionPerformed(evt);
  60.             }
  61.         });
  62.  
  63.         jLabel4.setText("Username");
  64.  
  65.         jButton2.setText("Cancel");
  66.  
  67.         jLabel5.setText("Password");
  68.  
  69.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  70.         getContentPane().setLayout(layout);
  71.         layout.setHorizontalGroup(
  72.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  73.             .addGroup(layout.createSequentialGroup()
  74.                 .addGap(28, 28, 28)
  75.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  76.                     .addComponent(jLabel1)
  77.                     .addComponent(jLabel4)
  78.                     .addComponent(jLabel5))
  79.                 .addGap(22, 22, 22)
  80.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  81.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  82.                         .addComponent(inputuserserv)
  83.                         .addComponent(inputpassserv, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
  84.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  85.                         .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  86.                             .addComponent(jButton1)
  87.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  88.                             .addComponent(jButton2))
  89.                         .addComponent(inputserver, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
  90.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  91.         );
  92.         layout.setVerticalGroup(
  93.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  94.             .addGroup(layout.createSequentialGroup()
  95.                 .addGap(29, 29, 29)
  96.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  97.                     .addComponent(jLabel1)
  98.                     .addComponent(inputserver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  99.                 .addGap(18, 18, 18)
  100.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  101.                     .addComponent(jLabel4)
  102.                     .addComponent(inputuserserv, 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.LEADING)
  105.                     .addComponent(jLabel5)
  106.                     .addComponent(inputpassserv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  107.                 .addGap(35, 35, 35)
  108.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  109.                     .addComponent(jButton1)
  110.                     .addComponent(jButton2))
  111.                 .addContainerGap(109, Short.MAX_VALUE))
  112.         );
  113.  
  114.         pack();
  115.     }// </editor-fold>                        
  116.  
  117.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  118.         // TODO add your handling code here:
  119.         try{
  120.             DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  121.             DriverManager.getConnection("jdbc:mysql://"+inputserver.getText(),inputuserserv.getText(),inputpassserv.getText());
  122.             JOptionPane.showMessageDialog(rootPane, "Berhasil melakukan koneksi!!!");
  123.             new FormLOGINUSER().show();
  124.             this.dispose();
  125.         } catch (HeadlessException | SQLException e){
  126.             JOptionPane.showMessageDialog(rootPane, "Gagal melakukan koneksi");
  127.         }
  128.     }                                        
  129.  
  130.     /**
  131.      * @param args the command line arguments
  132.      */
  133.     public static void main(String args[]) {
  134.         /* Set the Nimbus look and feel */
  135.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  136.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  137.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  138.          */
  139.         try {
  140.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  141.                 if ("Nimbus".equals(info.getName())) {
  142.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  143.                     break;
  144.                 }
  145.             }
  146.         } catch (ClassNotFoundException ex) {
  147.             java.util.logging.Logger.getLogger(Formkoneksi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  148.         } catch (InstantiationException ex) {
  149.             java.util.logging.Logger.getLogger(Formkoneksi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  150.         } catch (IllegalAccessException ex) {
  151.             java.util.logging.Logger.getLogger(Formkoneksi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  152.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  153.             java.util.logging.Logger.getLogger(Formkoneksi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  154.         }
  155.         //</editor-fold>
  156.  
  157.         /* Create and display the form */
  158.         java.awt.EventQueue.invokeLater(new Runnable() {
  159.             public void run() {
  160.                 new Formkoneksi().setVisible(true);
  161.             }
  162.         });
  163.     }
  164.  
  165.     // Variables declaration - do not modify                    
  166.     private javax.swing.JTextField inputpassserv;
  167.     private javax.swing.JTextField inputserver;
  168.     private javax.swing.JTextField inputuserserv;
  169.     private javax.swing.JButton jButton1;
  170.     private javax.swing.JButton jButton2;
  171.     private javax.swing.JLabel jLabel1;
  172.     private javax.swing.JLabel jLabel4;
  173.     private javax.swing.JLabel jLabel5;
  174.     // End of variables declaration                  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement