Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package database;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.HashSet;
- import java.util.Hashtable;
- public class Sites {
- private Connection connect = null;
- private Statement statement = null;
- //private PreparedStatement preparedStatement = null;
- private ResultSet resultSet = null;
- private String dbHost;
- private String dbName;
- private String dbUser;
- private String dbPass;
- private void read() throws Exception {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String dburl = "jdbc:mysql://" + dbHost + "/" + dbName +
- "?user=" + dbUser + "&password=" + dbPass;
- connect = DriverManager.getConnection(dburl);
- resultSet = statement.executeQuery("SELECT * from accounts.sites");
- } catch (Exception e) {
- throw e;
- }
- }
- public Hashtable<String, String> getSite(String domain)
- throws Exception, ExcSiteNodomain {
- Hashtable<String, String> table = new Hashtable<String, String>();
- boolean exists = false;
- try {
- read();
- while (resultSet.next()) {
- if (resultSet.getString("domain").equals(domain)) {
- exists = true;
- table.put("username", resultSet.getString("username"));
- table.put("domain", resultSet.getString("domain"));
- table.put("directory", resultSet.getString("directory"));
- table.put("fpm-template", resultSet.getString("fpm-template"));
- table.put("nginx-template", resultSet.getString("nginx-template"));
- }
- }
- if (!exists) {
- throw new ExcSiteNodomain(domain);
- }
- } catch (ExcSiteNodomain e) {
- throw e;
- } catch (Exception e) {
- throw e;
- } finally {
- resultSet.close();
- statement.close();
- connect.close();
- }
- return table;
- }
- public HashSet<String> listSites(String user)
- throws Exception, ExcSiteNosites {
- HashSet<String> list = new HashSet<String>();
- boolean exists = false;
- try {
- read();
- while (resultSet.next()) {
- if (resultSet.getString("username").equals(user)) {
- exists = true;
- list.add(resultSet.getString("domain"));
- }
- }
- if (!exists) {
- throw new ExcSiteNosites(user);
- }
- } catch (Exception e) {
- throw e;
- } finally {
- resultSet.close();
- statement.close();
- connect.close();
- }
- return list;
- }
- public Sites(String dbhost, String dbname, String dbuser, String dbpass) {
- dbHost = dbhost; dbName = dbname; dbUser = dbuser; dbPass = dbpass;
- }
- public Sites() {
- dbHost = "localhost";
- dbName = "accounts";
- dbUser = "accounts";
- dbPass = "sillybilly";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement