Advertisement
tpbolon

Untitled

Dec 10th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package ContohLaporan;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class DBConnection {
  9.  
  10.     private Connection conn = null;
  11.     static String DB_URL, DB_USER, DB_PASS, dbname, host, port;
  12.     boolean koneksi = false, db = false;
  13.  
  14.     public DBConnection() {
  15.         host = "localhost";
  16.         port = "3306";
  17.         dbname = "db_supermarket_siti3";
  18.         DB_USER = "root";
  19.         DB_PASS = "";
  20.         DB_URL = "jdbc:mysql://" + host + ":" + port + "/" + dbname;
  21.         if (conn == null) {
  22.             try {
  23.                 Class.forName("com.mysql.jdbc.Driver");
  24.                 conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
  25.                 koneksi = true;
  26.             } catch (SQLException ex) {
  27.                 JOptionPane.showMessageDialog(null,
  28.                         ex.getErrorCode() + ": " + ex.getMessage());
  29.                 koneksi = false;
  30.             } catch (ClassNotFoundException cne) {
  31.                 JOptionPane.showMessageDialog(null,
  32.                         "COM.MYSQL.JDBC.Driver tidak ditemukan\n" + cne.getMessage());
  33.                 koneksi = false;
  34.             }
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement