Advertisement
Guest User

Koneksi.java

a guest
May 29th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class Koneksi{
  8. private final String DRIVER = "com.mysql.jdbc.Driver";
  9. private final String DATABASE = "jdbc:mysql://localhost:3306/dbaplikasiperpustakaan";
  10. private final String USER = "root";
  11. private final String PASSWORD = "";
  12.  
  13. private Connection connection;
  14. private String pesanKesalahan;
  15.  
  16. public String getPesanKesalahan() {
  17. return pesanKesalahan;
  18. }
  19.  
  20. public Connection getConnection(){
  21. connection = null;
  22. pesanKesalahan = "";
  23.  
  24. try{
  25. Class.forName(DRIVER);
  26. } catch (ClassNotFoundException ex){
  27. pesanKesalahan = "JDBC Driver tidak ditemukan atau rusak\n"+ex;
  28. }
  29.  
  30. if (pesanKesalahan.equals("")){
  31. try{
  32. connection = DriverManager.getConnection(DATABASE+"?user="+USER+"&password="+PASSWORD+"");
  33. } catch (SQLException ex) {
  34. pesanKesalahan = "Koneksi ke "+DATABASE+" gagal\n"+ex;
  35. }
  36. }
  37. return connection;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement