Advertisement
Guest User

Untitled

a guest
May 24th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. package tamagotchi.data;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6. import tamagotchi.logic.*;
  7.  
  8. public class DatabaseConnector implements AutoCloseable {
  9.     public DatabaseConnector(String host, String database, String username, String password) throws SQLException {
  10.         dbHost = host;
  11.         dbDatabase = database;
  12.         dbUsername = username;
  13.         dbPassword = password;
  14.         connect();
  15.         initializeWrappers();
  16.     }
  17.  
  18.     private void initializeWrappers() {
  19.         usersTableWrapper = new UsersTableWrapper(this);
  20.         petTypesTableWrapper = new PetTypesTableWrapper(this);
  21.         petsTableWrapper = new PetsTableWrapper(this);
  22.         activitiesTableWrapper = new ActivitiesTableWrapper(this);
  23.         foodTypesTableWrapper = new FoodTypesTableWrapper(this);
  24.     }
  25.  
  26.     @Override
  27.     public void close() throws SQLException
  28.     {
  29.         try {
  30.             if (dbConnection != null && !dbConnection.isClosed())
  31.                 dbConnection.close();
  32.         } catch (SQLException ex) {
  33.             throw ex;
  34.         }
  35.     }
  36.    
  37.     public static DatabaseConnector create() throws SQLException {
  38.         try {
  39.             return new DatabaseConnector("localhost", "tamagotchi", "root", "");
  40.         } catch (SQLException ex) {
  41.             throw ex;
  42.         }
  43.     }
  44.  
  45.     private void connect() throws SQLException {
  46.         try {
  47.             dbConnection = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":3306/" + dbDatabase, dbUsername, dbPassword);
  48.         } catch (SQLException ex) {
  49.             throw ex;
  50.         }
  51.     }
  52.  
  53.     Connection getConnection() {
  54.         return dbConnection;
  55.     }
  56.  
  57.     public UsersTableWrapper getUsersWrapper() {
  58.         return usersTableWrapper;
  59.     }
  60.  
  61.     public PetTypesTableWrapper getPetTypesTableWrapper() {
  62.         return petTypesTableWrapper;
  63.     }
  64.  
  65.     public PetsTableWrapper getPetsTableWrapper() {
  66.         return petsTableWrapper;
  67.     }
  68.  
  69.     public ActivitiesTableWrapper getActivitiesTableWrapper() {
  70.         return activitiesTableWrapper;
  71.     }
  72.  
  73.     public FoodTypesTableWrapper getFoodTypesTableWrapper() {
  74.         return foodTypesTableWrapper;
  75.     }
  76.    
  77.     private String dbHost;
  78.     private String dbDatabase;
  79.     private String dbUsername;
  80.     private String dbPassword;
  81.     private Connection dbConnection;
  82.     private UsersTableWrapper usersTableWrapper;
  83.     private PetTypesTableWrapper petTypesTableWrapper;
  84.     private PetsTableWrapper petsTableWrapper;
  85.     private ActivitiesTableWrapper activitiesTableWrapper;
  86.     private FoodTypesTableWrapper foodTypesTableWrapper;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement