Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public void contextInitialized(ServletContextEvent sce) {
  2.  
  3. Properties serverProp = new Properties();
  4. try {
  5. serverProp.load(Files.newInputStream(Paths.get("webapps/webapp-baza/WEB-INF/dbsettings.properties"),
  6. StandardOpenOption.READ));
  7. } catch (IOException e) {
  8. System.out.println("Invalid server.properties file or file path");
  9. System.exit(1);
  10. }
  11. String dbName = serverProp.getProperty("name");
  12. String host = serverProp.getProperty("host");
  13. String port = serverProp.getProperty("port");
  14. String user = serverProp.getProperty("user");
  15. String password = serverProp.getProperty("password");
  16.  
  17. String connectionURL = "jdbc:derby://" + host + ":" + port + "/" + dbName + ";user=" + user + ";" + "password="
  18. + password;
  19.  
  20. ComboPooledDataSource cpds = new ComboPooledDataSource();
  21. try {
  22. cpds.setDriverClass("org.apache.derby.jdbc.ClientDriver");
  23. } catch (PropertyVetoException e1) {
  24. throw new RuntimeException("Pogreška prilikom inicijalizacije poola.", e1);
  25. }
  26. cpds.setJdbcUrl(connectionURL);
  27.  
  28. try {
  29. Connection con = cpds.getConnection();
  30. SQLConnectionProvider.setConnection(con);
  31.  
  32. SQLDAO dao = (SQLDAO) DAOProvider.getDao();
  33. dao.checkTableExistence();
  34.  
  35. } catch (Exception e) {
  36. throw new IllegalArgumentException(e.getMessage());
  37. } finally {
  38. SQLConnectionProvider.setConnection(null);
  39. }
  40.  
  41. sce.getServletContext().setAttribute("hr.fer.zemris.dbpool", cpds);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement