SHARE
TWEET

KoneksiDB

RendraTriyanto Apr 18th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package koneksi;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class KoneksiDB {
  10.     private Connection koneksi;
  11.     private ResultSet rs;
  12.     private PreparedStatement ps;
  13.    
  14.     public Connection getKoneksi() {
  15.         if (koneksi == null) {
  16.             try {
  17.                 Class.forName("com.mysql.jdbc.Driver");
  18.                 try {
  19.                     String url = "jdbc:mysql://localhost:3306/coffedb";
  20.                     koneksi = DriverManager.getConnection(url, "root", "");
  21.                     System.out.println("YEY, Koneksi Sukses");
  22.                 } catch (SQLException se) {
  23.                     System.out.println("YAHH, Koneksi Gagal" + se);
  24.                     System.exit(0);
  25.                 }
  26.             } catch (ClassNotFoundException cnfe) {
  27.                 System.out.println("Maaf, Class tidak ditemukan !" + cnfe);
  28.                 System.exit(0);
  29.             }
  30.         }
  31.         return koneksi;
  32.     }
  33.  
  34.     public ResultSet getRs() {
  35.         return rs;
  36.     }
  37.  
  38.     public boolean eksekusiQuery(String query, boolean baris) {
  39.         try {
  40.             ps = koneksi.prepareStatement(query);
  41.             if (baris) {
  42.                 rs = ps.executeQuery();
  43.             } else {
  44.                 ps.executeUpdate();
  45.             }
  46.             return true;
  47.         } catch (SQLException e) {
  48.             return false;
  49.         }
  50.     }
  51.  
  52.     public static void main(String[] coba) {
  53.         new KoneksiDB().getKoneksi();
  54.     }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top