Advertisement
Sucahyo4

DBConnection

Jan 10th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import javax.swing.JOptionPane;
  5. import javax.swing.JTable;
  6. import javax.swing.table.DefaultTableModel;
  7. /**
  8. * @author Jie
  9. */
  10. public class DBConnection {
  11. private Connection koneksi; // variabel koneksi
  12. private boolean status_koneksi; //Status koneksi sukses / gagal
  13. DefaultTableModel DftTabMode;
  14. public Connection connect(){
  15. //untuk koneksi ke driver
  16. try{
  17. Class.forName("com.mysql.jdbc.Driver"); //load driver jdbc untuk koneksi dengan database MySQL
  18. // System.out.println("berhasil load driver");
  19. }catch(ClassNotFoundException cnfe){
  20. System.out.println("Tidak ada Driver "+cnfe);
  21. }
  22. //untuk koneksi ke database
  23. try{
  24. String url="jdbc:mysql://localhost:3306/Androidmobile"; //Host dan dan database
  25. koneksi=DriverManager.getConnection(url,"root",""); //User dan password untuk koneksi ke database
  26. System.out.println("Berhasil koneksi");
  27. status_koneksi = true;
  28. }catch(SQLException se){
  29. // System.out.println("Gagal koneksi "+se);
  30. status_koneksi = false;
  31. JOptionPane.showMessageDialog(null,"Gagal koneksi...MySQL server belum aktif");
  32. }
  33. return koneksi;
  34. }
  35. /*
  36. Mengambil status koneksi
  37. */
  38. public boolean getConnection_status(){
  39. return this.status_koneksi;
  40. }
  41. /*
  42. Parameter
  43. jTableName : sebagai variabel parameter untuk nama tabel
  44. */
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement