Advertisement
Guest User

Sites.java

a guest
Feb 26th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. import java.util.HashSet;
  8. import java.util.Hashtable;
  9.  
  10. public class Sites {
  11.  
  12.     private Connection connect = null;
  13.     private Statement statement = null;
  14.     //private PreparedStatement preparedStatement = null;
  15.     private ResultSet resultSet = null;
  16.     private String dbHost;
  17.     private String dbName;
  18.     private String dbUser;
  19.     private String dbPass;
  20.    
  21.     private void read() throws Exception {
  22.         try {  
  23.             Class.forName("com.mysql.jdbc.Driver");
  24.        
  25.             String dburl = "jdbc:mysql://" + dbHost + "/" + dbName +
  26.                             "?user=" + dbUser + "&password=" + dbPass;
  27.            
  28.             connect = DriverManager.getConnection(dburl);
  29.            
  30.             resultSet = statement.executeQuery("SELECT * from accounts.sites");
  31.         } catch (Exception e) {
  32.             throw e;
  33.         }
  34.     }
  35.    
  36.     public Hashtable<String, String> getSite(String domain)
  37.             throws Exception, ExcSiteNodomain {
  38.         Hashtable<String, String> table = new Hashtable<String, String>();
  39.         boolean exists = false;
  40.        
  41.         try {
  42.             read();
  43.            
  44.             while (resultSet.next()) {
  45.                 if (resultSet.getString("domain").equals(domain)) {
  46.                     exists = true;
  47.                     table.put("username", resultSet.getString("username"));
  48.                     table.put("domain", resultSet.getString("domain"));
  49.                     table.put("directory", resultSet.getString("directory"));
  50.                     table.put("fpm-template", resultSet.getString("fpm-template"));
  51.                     table.put("nginx-template", resultSet.getString("nginx-template"));
  52.                 }
  53.             }
  54.            
  55.             if (!exists) {
  56.                 throw new ExcSiteNodomain(domain);
  57.             }
  58.         } catch (ExcSiteNodomain e) {
  59.             throw e;
  60.         } catch (Exception e) {
  61.             throw e;
  62.         } finally {
  63.             resultSet.close();
  64.             statement.close();
  65.             connect.close();
  66.         }
  67.        
  68.         return table;
  69.     }
  70.    
  71.     public HashSet<String> listSites(String user)
  72.             throws Exception, ExcSiteNosites {
  73.         HashSet<String> list = new HashSet<String>();
  74.         boolean exists = false;
  75.        
  76.         try {
  77.             read();
  78.            
  79.             while (resultSet.next()) {
  80.                 if (resultSet.getString("username").equals(user)) {
  81.                     exists = true;
  82.                     list.add(resultSet.getString("domain"));
  83.                 }
  84.             }
  85.            
  86.             if (!exists) {
  87.                 throw new ExcSiteNosites(user);
  88.             }
  89.         } catch (Exception e) {
  90.             throw e;
  91.         } finally {
  92.             resultSet.close();
  93.             statement.close();
  94.             connect.close();
  95.         }
  96.        
  97.         return list;
  98.     }
  99.        
  100.     public Sites(String dbhost, String dbname, String dbuser, String dbpass) {
  101.         dbHost = dbhost; dbName = dbname; dbUser = dbuser; dbPass = dbpass;
  102.     }
  103.    
  104.     public Sites() {
  105.         dbHost = "localhost";
  106.         dbName = "accounts";
  107.         dbUser = "accounts";
  108.         dbPass = "sillybilly";
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement