Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tamagotchi.data;
- import java.sql.*;
- import java.util.ArrayList;
- import tamagotchi.logic.*;
- public class DatabaseConnector implements AutoCloseable {
- public DatabaseConnector(String host, String database, String username, String password) throws SQLException {
- dbHost = host;
- dbDatabase = database;
- dbUsername = username;
- dbPassword = password;
- connect();
- initializeWrappers();
- }
- private void initializeWrappers() {
- usersTableWrapper = new UsersTableWrapper(this);
- petTypesTableWrapper = new PetTypesTableWrapper(this);
- petsTableWrapper = new PetsTableWrapper(this);
- activitiesTableWrapper = new ActivitiesTableWrapper(this);
- foodTypesTableWrapper = new FoodTypesTableWrapper(this);
- }
- @Override
- public void close() throws SQLException
- {
- try {
- if (dbConnection != null && !dbConnection.isClosed())
- dbConnection.close();
- } catch (SQLException ex) {
- throw ex;
- }
- }
- public static DatabaseConnector create() throws SQLException {
- try {
- return new DatabaseConnector("localhost", "tamagotchi", "root", "");
- } catch (SQLException ex) {
- throw ex;
- }
- }
- private void connect() throws SQLException {
- try {
- dbConnection = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":3306/" + dbDatabase, dbUsername, dbPassword);
- } catch (SQLException ex) {
- throw ex;
- }
- }
- Connection getConnection() {
- return dbConnection;
- }
- public UsersTableWrapper getUsersWrapper() {
- return usersTableWrapper;
- }
- public PetTypesTableWrapper getPetTypesTableWrapper() {
- return petTypesTableWrapper;
- }
- public PetsTableWrapper getPetsTableWrapper() {
- return petsTableWrapper;
- }
- public ActivitiesTableWrapper getActivitiesTableWrapper() {
- return activitiesTableWrapper;
- }
- public FoodTypesTableWrapper getFoodTypesTableWrapper() {
- return foodTypesTableWrapper;
- }
- private String dbHost;
- private String dbDatabase;
- private String dbUsername;
- private String dbPassword;
- private Connection dbConnection;
- private UsersTableWrapper usersTableWrapper;
- private PetTypesTableWrapper petTypesTableWrapper;
- private PetsTableWrapper petsTableWrapper;
- private ActivitiesTableWrapper activitiesTableWrapper;
- private FoodTypesTableWrapper foodTypesTableWrapper;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement