tpbolon

Java Koneksi

Jul 16th, 2020
1,019
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import javax.swing.JOptionPane;
  7.  
  8. /*
  9.  * To change this license header, choose License Headers in Project Properties.
  10.  * To change this template file, choose Tools | Templates
  11.  * and open the template in the editor.
  12.  */
  13.  
  14. /**
  15.  *
  16.  * @author chand
  17.  */
  18. public class frameInventaris extends javax.swing.JFrame {
  19.     private Statement st;
  20.     private Connection Con;
  21.     private ResultSet Rs;
  22.     private String Sql="";
  23.  
  24.     /**
  25.      * Creates new form frameInventaris
  26.      */
  27.     public frameInventaris() {
  28.         initComponents();
  29.         KoneksiKeDatabase(); //memanggil prosedur koneksi bila program dijalankan
  30.     }
  31.    
  32.     private void KoneksiKeDatabase(){
  33.         try {
  34.             //untuk menentukan driver database
  35.             Class.forName("com.mysql.jdbc.Driver");
  36.             //untuk membuat koneksi dari database ke driver
  37.             Con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_inventori",
  38.                     "root", "");
  39.             //untuk memberikan pesan apabila koneksi telah berhasil
  40.             JOptionPane.showMessageDialog(null, "Selamat Koneksi Berhasil");
  41.         } catch (Exception e) {
  42.             //untuk memberikan pesan ditaskbar bila error
  43.             System.out.println("Koneksi Gagal "+e.getMessage());
  44.         }
  45.     }
  46.  
  47.     /**
  48.      * This method is called from within the constructor to initialize the form.
  49.      * WARNING: Do NOT modify this code. The content of this method is always
  50.      * regenerated by the Form Editor.
  51.      */
  52.     @SuppressWarnings("unchecked")
  53.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  54.     private void initComponents() {
  55.  
  56.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  57.  
  58.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  59.         getContentPane().setLayout(layout);
  60.         layout.setHorizontalGroup(
  61.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  62.             .addGap(0, 400, Short.MAX_VALUE)
  63.         );
  64.         layout.setVerticalGroup(
  65.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  66.             .addGap(0, 300, Short.MAX_VALUE)
  67.         );
  68.  
  69.         pack();
  70.     }// </editor-fold>                        
  71.  
  72.     /**
  73.      * @param args the command line arguments
  74.      */
  75.     public static void main(String args[]) {
  76.         /* Set the Nimbus look and feel */
  77.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  78.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  79.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  80.          */
  81.         try {
  82.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  83.                 if ("Nimbus".equals(info.getName())) {
  84.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  85.                     break;
  86.                 }
  87.             }
  88.         } catch (ClassNotFoundException ex) {
  89.             java.util.logging.Logger.getLogger(frameInventaris.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  90.         } catch (InstantiationException ex) {
  91.             java.util.logging.Logger.getLogger(frameInventaris.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  92.         } catch (IllegalAccessException ex) {
  93.             java.util.logging.Logger.getLogger(frameInventaris.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  94.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  95.             java.util.logging.Logger.getLogger(frameInventaris.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  96.         }
  97.         //</editor-fold>
  98.  
  99.         /* Create and display the form */
  100.         java.awt.EventQueue.invokeLater(new Runnable() {
  101.             public void run() {
  102.                 new frameInventaris().setVisible(true);
  103.             }
  104.         });
  105.     }
  106.  
  107.     // Variables declaration - do not modify                    
  108.     // End of variables declaration                  
  109. }
RAW Paste Data