Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package medivation.domain.JavaClasses;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. /**
  8. * Created by c1324725 on 29/11/2016.
  9. */
  10. public class ConnectionManager {
  11. private static String url = "jdbc:mysql://localhost:3306/medivation";
  12. private static String driverName = "com.mysql.jdbc.Driver";
  13. private static String username = "root";
  14. private static String password = "abc123";
  15. private static Connection con;
  16.  
  17. public static Connection getConnection() {
  18. try {
  19. Class.forName(driverName);
  20. try {
  21. con = DriverManager.getConnection(url, username, password);
  22. } catch (SQLException ex) {
  23. // log an exception. fro example:
  24. System.out.println("Failed to create the database connection.");
  25. }
  26. } catch (ClassNotFoundException ex) {
  27. // log an exception. for example:
  28. System.out.println("Driver not found.");
  29. }
  30. return con;
  31. }
  32.  
  33.  
  34. public static void closeConnection(){
  35. try {
  36. if(con != null)
  37. {
  38. con.close();
  39. System.out.println("Connection closed");
  40. }else {
  41. System.out.println("Connection already closed");
  42. }
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement