package conferenceOrganisation.database.connection;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.ejb.Singleton;

@Singleton
public class DatabaseConnection {

	public Statement createStatement() throws SQLException, IOException {

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("No driver!");
		}

		Connection connection = null;

		try {
			Properties prop = new Properties();
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader
					.getResourceAsStream("conferenceOrganisation/database/connection/config.properties");

			prop.load(input);

			String databaseUrl = String.format("jdbc:mysql://%s:%s/%s", prop.getProperty("databaseNetworkAddress"),
					prop.getProperty("databasePort"), prop.getProperty("databaseName"));

			connection = DriverManager.getConnection(databaseUrl, prop.getProperty("databaseUserAccount"),
					prop.getProperty("databasePassword"));
		} catch (SQLException e) {
			System.out.println("Connection failed");
		}

		if (connection != null) {
			System.out.println("Connected successfuly to the database.");
		} else {
			System.out.println("Connection failed.");
		}

		return connection.createStatement();
	}
}

@@ -329,39 +329,42 @@
 %0A%0A%09p
-ublic Statement createStatement
+rivate Connection createConnection
 () t
@@ -369,19 +369,18 @@
  throws 
-SQL
+IO
 Exceptio
@@ -382,18 +382,19 @@
 eption, 
-IO
+SQL
 Exceptio
@@ -397,17 +397,16 @@
 ption %7B%0A
-%0A
 %09%09try %7B%0A
@@ -562,25 +562,16 @@
  null;%0A%0A
-%09%09try %7B%0A%09
 %09%09Proper
@@ -600,17 +600,16 @@
 ties();%0A
-%09
 %09%09ClassL
@@ -678,17 +678,16 @@
 er();%0A%09%09
-%09
 InputStr
@@ -710,17 +710,16 @@
 sLoader%0A
-%09
 %09%09%09%09.get
@@ -803,17 +803,16 @@
 s%22);%0A%0A%09%09
-%09
 prop.loa
@@ -822,17 +822,16 @@
 nput);%0A%0A
-%09
 %09%09String
@@ -924,33 +924,32 @@
 kAddress%22),%0A%09%09%09%09
-%09
 prop.getProperty
@@ -1002,17 +1002,16 @@
 me%22));%0A%0A
-%09
 %09%09connec
@@ -1103,17 +1103,16 @@
 %22),%0A%09%09%09%09
-%09
 prop.get
@@ -1146,90 +1146,159 @@
 ));%0A
+%0A
 %09%09
-%7D catch (SQLException e) %7B%0A%09%09%09System.out.println(%22Connection failed%22);%0A%09%09%7D%0A%0A%09%09if
+return connection;%0A%09%7D%0A%0A%09public Statement createStatement() throws SQLException, IOException %7B%0A%0A%09%09Connection connection = createConnection();%0A%09%09while
  (co
@@ -1306,17 +1306,17 @@
 nection 
-!
+=
 = null) 
@@ -1351,55 +1351,64 @@
 nect
-ed successfuly to the database.%22);%0A%09%09%7D else %7B%0A%09
+ion failed.%22);%0A%09%09%09connection = createConnection();%0A%09%09%7D%0A%0A
 %09%09Sy
@@ -1440,23 +1440,39 @@
 ion 
-failed.%22);%0A%09%09%7D%0A
+successfuly to the database.%22);
 %0A%09%09r

fix random db connection with very ugly code