package com.zhiyou100.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public enum DBUtil {
INSTANCE;
private String databaseName;
private String url;
private String username;
private String password;
private DBUtil() {
try (InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("data.properties");){
Properties properties = new Properties();
properties.load(is);
databaseName = properties.getProperty("databaseName");
url = properties.getProperty("url") + databaseName;
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
public Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}