Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package pl.polsl.java.lab6.model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import javax.servlet.ServletContext;
  7.  
  8. /**
  9. * Enable creating single database con during using of application.
  10. *
  11. * @author rdule
  12. * @version 1.0
  13. */
  14. public class DatabaseConnection {
  15.  
  16. private static Connection con = null;
  17.  
  18. /**
  19. * Method creates single database con
  20. *
  21. * @param context Gets con params.
  22. * @return Connection with database.
  23. * @throws SQLException Causes by SQL issues.
  24. * @throws ClassNotFoundException Driver not found.
  25. */
  26. public static synchronized Connection connectWithDatabase(ServletContext context) throws SQLException, ClassNotFoundException {
  27. if (con == null) {
  28.  
  29. String driverPath = context.getInitParameter("driverUrl");
  30. Class.forName(driverPath);
  31. String databaseURL = context.getInitParameter("databaseURL");
  32. String username = context.getInitParameter("username");
  33. String password = context.getInitParameter("password");
  34.  
  35. con = DriverManager.getConnection(databaseURL, username, password);
  36.  
  37. }
  38. return con;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement