package nuclibook.server;

import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import nuclibook.constants.C;
import nuclibook.models.*;

import java.sql.SQLException;

public class SqlServerConnection {

	/* singleton pattern */

	private SqlServerConnection() {
	}

	private static ConnectionSource connection = null;

	public static ConnectionSource acquireConnection() {
		if (connection == null) {
			try {
				connection = new JdbcConnectionSource(C.MYSQL_URI);
				((JdbcConnectionSource) connection).setUsername(C.MYSQL_USERNAME);
				((JdbcConnectionSource) connection).setPassword(C.MYSQL_PASSWORD);
				initDB(connection);
			} catch (Exception e) {
				e.printStackTrace(); // TODO deal with exception
			}
		}

		return connection;
	}

	public static void initDB(ConnectionSource connection) {
		try {
			TableUtils.createTableIfNotExists(connection, ActionLog.class);
			TableUtils.createTableIfNotExists(connection, Booking.class);
			TableUtils.createTableIfNotExists(connection, BookingStaff.class);
			TableUtils.createTableIfNotExists(connection, Camera.class);
			TableUtils.createTableIfNotExists(connection, CameraType.class);
			TableUtils.createTableIfNotExists(connection, Medicine.class);
			TableUtils.createTableIfNotExists(connection, Patient.class);
			TableUtils.createTableIfNotExists(connection, Permission.class);
			TableUtils.createTableIfNotExists(connection, Staff.class);
			TableUtils.createTableIfNotExists(connection, StaffAbsences.class);
			TableUtils.createTableIfNotExists(connection, StaffAvailability.class);
			TableUtils.createTableIfNotExists(connection, StaffRole.class);
			TableUtils.createTableIfNotExists(connection, StaffRolePermission.class);
			TableUtils.createTableIfNotExists(connection, Therapy.class);
		} catch (SQLException e) {
			e.printStackTrace(); // TODO deal with exception
		}
	}
}

@@ -458,16 +458,195 @@
 ion() %7B%0A
+%09%09return acquireConnection(C.MYSQL_URI, C.MYSQL_PASSWORD, C.MYSQL_PASSWORD);%0A%09%7D%0A%0A%09public static ConnectionSource acquireConnection(String URI, String username, String password) %7B%0A
 %09%09if (co
@@ -716,24 +716,16 @@
 nSource(
-C.MYSQL_
 URI);%0A%09%09
@@ -778,24 +778,16 @@
 ame(
-C.MYSQL_USERNAME
+username
 );%0A%09
@@ -837,32 +837,24 @@
 assword(
-C.MYSQL_PASSWORD
+password
 );%0A%09%09%09%09i

Make the acquireConnection configurable with different MySQL connection settings so that a different database can be specified for unit testing.