Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package utils;
- import java.io.FileReader;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.util.Properties;
- public class MySqlConnection {
- private Connection dbConnection;
- private Properties properties = new Properties();
- private String url;
- private String username;
- private String password;
- public MySqlConnection() {
- connectMySql();
- }
- private void connectMySql() {
- setParametars();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- dbConnection = DriverManager.getConnection(url, username, password);
- if (dbConnection == null) {
- System.out.println("Connection ERROR!");
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- public Connection getMySqlConnection() {
- return dbConnection;
- }
- private void setParametars(){
- try {
- properties.load(new FileReader("db.config"));
- } catch (IOException e) {
- System.out.println("Cannot find database.config " + e);
- }
- url = properties.getProperty("db.url");
- username = properties.getProperty("db.username");
- if(properties.getProperty("db.password").equals("0"))
- password = "";
- else password = properties.getProperty("db.password");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement