Advertisement
Guest User

Untitled

a guest
May 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. package utils;
  2.  
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.util.Properties;
  8.  
  9. public class MySqlConnection {
  10. private Connection dbConnection;
  11. private Properties properties = new Properties();
  12. private String url;
  13. private String username;
  14. private String password;
  15.  
  16.  
  17. public MySqlConnection() {
  18. connectMySql();
  19. }
  20.  
  21. private void connectMySql() {
  22. setParametars();
  23. try {
  24. Class.forName("com.mysql.jdbc.Driver");
  25. dbConnection = DriverManager.getConnection(url, username, password);
  26. if (dbConnection == null) {
  27. System.out.println("Connection ERROR!");
  28. }
  29. } catch (Exception e) {
  30. System.out.println(e);
  31. }
  32. }
  33.  
  34. public Connection getMySqlConnection() {
  35. return dbConnection;
  36. }
  37.  
  38. private void setParametars(){
  39. try {
  40. properties.load(new FileReader("db.config"));
  41. } catch (IOException e) {
  42. System.out.println("Cannot find database.config " + e);
  43. }
  44. url = properties.getProperty("db.url");
  45. username = properties.getProperty("db.username");
  46. if(properties.getProperty("db.password").equals("0"))
  47. password = "";
  48. else password = properties.getProperty("db.password");
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement