Advertisement
Cloudjumper

Untitled

Dec 1st, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.82 KB | None | 0 0
  1. package org.cloudjumper.db.config;
  2.  
  3. import org.hibernate.cfg.Configuration;
  4. import org.json.JSONObject;
  5. import org.springframework.context.annotation.Scope;
  6. import org.springframework.core.io.ClassPathResource;
  7. import org.springframework.stereotype.Component;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.util.stream.Collectors;
  13.  
  14. /**
  15.  * Created by cloudjumper on 11/7/16.
  16.  */
  17. @Component
  18. @Scope(value = "singleton")
  19. public class HibernateConfig {
  20.     private Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
  21.  
  22.     public HibernateConfig() {
  23.         JSONObject jsonConfig;
  24.         String file;
  25.         try {
  26. //            file = new String(Files.readAllBytes(Paths.get("src/main/resources/Config.json")), StandardCharsets.UTF_8);
  27. //            Resource resource = new ClassPathResource("Config.json");
  28. //            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
  29. //            file = bufferedReader.lines().collect(Collectors.joining("\n"));
  30.             file = new BufferedReader(new InputStreamReader(new ClassPathResource("Config.json").getInputStream()))
  31.                     .lines()
  32.                     .collect(Collectors.joining("\n"));
  33.             jsonConfig = new JSONObject(file);
  34.             configuration.setProperty("hibernate.connection.url","jdbc:mysql://localhost:3306/void");
  35.             configuration.setProperty("hibernate.connection.username",jsonConfig.getString("db_user"));
  36.             configuration.setProperty("hibernate.connection.password",jsonConfig.getString("db_pass"));
  37.         } catch (IOException e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.  
  42.     public Configuration getConfiguration() {
  43.         return configuration;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement