SHARE
TWEET

KoneksiDB

a guest 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. /**
  10.  *
  11.  * @author Rendra Triyanto
  12.  */
  13. public class KoneksiDB {
  14.     private Connection koneksi;
  15.     private ResultSet rs;
  16.     private PreparedStatement ps;
  17.    
  18.     public Connection getKoneksi() {
  19.         if (koneksi == null) {
  20.             try {
  21.                 Class.forName("com.mysql.jdbc.Driver");
  22.                 try {
  23.                     String url = "jdbc:mysql://localhost:3306/coffedb";
  24.                     koneksi = DriverManager.getConnection(url, "root", "");
  25.                     System.out.println("YEY, Koneksi Sukses");
  26.                 } catch (SQLException se) {
  27.                     System.out.println("YAHH, Koneksi Gagal" + se);
  28.                     System.exit(0);
  29.                 }
  30.             } catch (ClassNotFoundException cnfe) {
  31.                 System.out.println("Maaf, Class tidak ditemukan !" + cnfe);
  32.                 System.exit(0);
  33.             }
  34.  
  35.         }
  36.         return koneksi;
  37.     }
  38.  
  39.     public ResultSet getRs() {
  40.         return rs;
  41.     }
  42.  
  43.     public boolean eksekusiQuery(String query, boolean baris) {
  44.         try {
  45.             ps = koneksi.prepareStatement(query);
  46.             if (baris) {
  47.                 rs = ps.executeQuery();
  48.             } else {
  49.                 ps.executeUpdate();
  50.             }
  51.             return true;
  52.         } catch (SQLException e) {
  53.             return false;
  54.         }
  55.     }
  56.  
  57.     public static void main(String[] coba) {
  58.         new KoneksiDB().getKoneksi();
  59.     }
  60.    
  61. }
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