Advertisement
stevennathaniel

Kelas Koneksi Ke Database PostgreSQL: OOP

Feb 4th, 2015
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.14 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. package latihanbasic2;
  7.  
  8.  
  9. import java.sql.Connection;
  10.  
  11. import java.sql.DriverManager;
  12.  
  13. import java.sql.SQLException;
  14.  
  15. import java.util.Properties;
  16.  
  17. /**
  18.  *
  19.  * @author steven
  20.  */
  21.  
  22.  
  23. class Menyambung{
  24.    
  25.     public void Sambungan(){
  26.        
  27.        
  28.     try {
  29.    
  30.     Connection Koneksi = null;
  31.  
  32.     String url = "jdbc:postgresql://localhost:5432/pdam";
  33.    
  34.     String user = "steven";
  35.    
  36.     String password = "kucing";
  37.    
  38.     Koneksi = DriverManager.getConnection(url,user,password);
  39.    
  40.     if(Koneksi != null);
  41.    
  42.     System.out.println("Berhasil Terkoneksi ke Database");
  43.    
  44.     Koneksi.close();
  45.    
  46.  
  47.  
  48.    
  49.    
  50. } catch(SQLException ex){
  51.  
  52.  
  53. ex.printStackTrace();
  54.  
  55. }
  56.  
  57.    
  58.    
  59. }
  60.  
  61.     }
  62.  
  63. public class BasicClass4 extends javax.swing.JFrame {
  64.  
  65.     /**
  66.      * Creates new form BasicClass4
  67.      */
  68.     public BasicClass4() {
  69.         initComponents();
  70.     }
  71.  
  72.     /**
  73.      * This method is called from within the constructor to initialize the form.
  74.      * WARNING: Do NOT modify this code. The content of this method is always
  75.      * regenerated by the Form Editor.
  76.      */
  77.     @SuppressWarnings("unchecked")
  78.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  79.     private void initComponents() {
  80.  
  81.         jButton1 = new javax.swing.JButton();
  82.  
  83.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  84.  
  85.         jButton1.setFont(new java.awt.Font("Cantarell", 1, 15)); // NOI18N
  86.         jButton1.setText("Test Koneksi");
  87.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  88.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  89.                 jButton1ActionPerformed(evt);
  90.             }
  91.         });
  92.  
  93.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  94.         getContentPane().setLayout(layout);
  95.         layout.setHorizontalGroup(
  96.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  97.             .addGroup(layout.createSequentialGroup()
  98.                 .addContainerGap()
  99.                 .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
  100.                 .addContainerGap(244, Short.MAX_VALUE))
  101.         );
  102.         layout.setVerticalGroup(
  103.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  104.             .addGroup(layout.createSequentialGroup()
  105.                 .addContainerGap()
  106.                 .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
  107.                 .addContainerGap(234, Short.MAX_VALUE))
  108.         );
  109.  
  110.         pack();
  111.     }// </editor-fold>                        
  112.  
  113.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  114.         // TODO add your handling code here:
  115.        
  116.        
  117.     Menyambung obj = new Menyambung();
  118.    
  119.     obj.Sambungan();
  120.        
  121.        
  122.    
  123.        
  124.     }                                        
  125.  
  126.     /**
  127.      * @param args the command line arguments
  128.      */
  129.     public static void main(String args[]) {
  130.         /* Set the Nimbus look and feel */
  131.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  132.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  133.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  134.          */
  135.         try {
  136.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  137.                 if ("Nimbus".equals(info.getName())) {
  138.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  139.                     break;
  140.                 }
  141.             }
  142.         } catch (ClassNotFoundException ex) {
  143.             java.util.logging.Logger.getLogger(BasicClass4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  144.         } catch (InstantiationException ex) {
  145.             java.util.logging.Logger.getLogger(BasicClass4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  146.         } catch (IllegalAccessException ex) {
  147.             java.util.logging.Logger.getLogger(BasicClass4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  148.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  149.             java.util.logging.Logger.getLogger(BasicClass4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  150.         }
  151.         //</editor-fold>
  152.  
  153.         /* Create and display the form */
  154.         java.awt.EventQueue.invokeLater(new Runnable() {
  155.             public void run() {
  156.                 new BasicClass4().setVisible(true);
  157.             }
  158.         });
  159.     }
  160.  
  161.     // Variables declaration - do not modify                    
  162.     private javax.swing.JButton jButton1;
  163.     // End of variables declaration                  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement