Guest User

Untitled

a guest
Apr 22nd, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. package dblink;
  2.  
  3. import java.sql.DriverManager;
  4. import javax.swing.JOptionPane;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Connection;
  8.  
  9. public class DBConnectie {
  10.  
  11. private static DBConnectie database;
  12. private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  13. private static final String DATABASE_URL = "jdbc:mysql://mysqlha2.ugent.be/BINFtest069";
  14. private Connection connection;
  15.  
  16. public static DBConnectie getInstance() {
  17. if (database == null) {
  18. database = new DBConnectie();
  19. }
  20. return database;
  21. }
  22.  
  23. public DBConnectie() {
  24. try {
  25. Class.forName(JDBC_DRIVER);
  26. connection = DriverManager.getConnection(DATABASE_URL, "BINFtest069", "RlrBjF2Hcx");
  27. } catch (SQLException sqlException) {
  28. JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Error while connection to the database", JOptionPane.ERROR_MESSAGE);
  29. System.exit(1);
  30. } catch (ClassNotFoundException classNotFound) {
  31. JOptionPane.showMessageDialog(null, classNotFound.getMessage(), "Could not load driver", JOptionPane.ERROR_MESSAGE);
  32. System.exit(1);
  33. }
  34. }
  35.  
  36. public Connection getConnection() {
  37. return connection;
  38. }
  39.  
  40. public void closeConnection() {
  41. try {
  42. connection.close();
  43. } catch (SQLException sqlException) {
  44. JOptionPane.showMessageDialog(null, sqlException.getMessage(), "Cannot disconnect database", JOptionPane.ERROR_MESSAGE);
  45. System.exit(0);
  46. }
  47. }
  48.  
  49. public ResultSet selectQuery(String sql) {
  50. try {
  51. return connection.createStatement().executeQuery(sql);
  52. } catch (SQLException e) {
  53. return null;
  54. }
  55. }
  56.  
  57. public int insertQuery(String sql) {
  58. try {
  59. return connection.createStatement().executeUpdate(sql);
  60. } catch (SQLException e) {
  61. JOptionPane.showMessageDialog(null, e.getMessage());
  62. return 0;
  63. }
  64. }
  65. }
Add Comment
Please, Sign In to add comment