From: Roland Haeder Date: Wed, 29 Jul 2015 12:51:21 +0000 (+0200) Subject: Fixed and added more MySQL-related stuff X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ed8b0a97cddc53e44a3f55948c9214081b5ca37a;p=jaddressbook-share-lib.git Fixed and added more MySQL-related stuff Signed-off-by:Roland Häder --- diff --git a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java index f5e8f05..5099196 100644 --- a/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java +++ b/Addressbook/src/org/mxchange/addressbook/BaseFrameworkSystem.java @@ -22,6 +22,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.text.MessageFormat; import java.util.Properties; import java.util.ResourceBundle; import org.apache.logging.log4j.LogManager; @@ -125,10 +126,19 @@ public class BaseFrameworkSystem implements FrameworkInterface { * Prepares all properties, the file is written if it is not found */ private void initProperties () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + try { // Try to read it this.properties.load(new BufferedReader(new InputStreamReader(new FileInputStream(FrameworkInterface.PROPERTIES_CONFIG_FILE)))); + + // Debug message + this.getLogger().debug(MessageFormat.format("{0} properties has been loaded.", this.properties.size())); //NOI18N } catch (final FileNotFoundException ex) { + // Debug message + this.getLogger().debug(MessageFormat.format("Properties file {0} not found: {1}", FrameworkInterface.PROPERTIES_CONFIG_FILE, ex)); //NOI18N + /* * The file is not found which is normal for first run, so * initialize default values. @@ -141,32 +151,48 @@ public class BaseFrameworkSystem implements FrameworkInterface { // Something else didn't work this.abortProgramWithException(ex); } + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N } /** * Initializes properties with default values */ private void initPropertiesWithDefault () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + // Init default values: // Default database backend - this.properties.put("org.mxchange.addressbook.database.backendType", "base64csv"); + this.properties.put("org.mxchange.addressbook.database.backendType", "base64csv"); //NOI18N // For MySQL backend - this.properties.put("org.mxchange.addressbook.database.mysql.host", "localhost"); - this.properties.put("org.mxchange.addressbook.database.mysql.login", ""); - this.properties.put("org.mxchange.addressbook.database.mysql.password", ""); + this.properties.put("org.mxchange.addressbook.database.mysql.host", "localhost"); //NOI18N + this.properties.put("org.mxchange.addressbook.database.mysql.dbname", "test"); //NOI18N + this.properties.put("org.mxchange.addressbook.database.mysql.login", ""); //NOI18N + this.properties.put("org.mxchange.addressbook.database.mysql.password", ""); //NOI18N + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N } /** * Writes the properties file to disk */ private void writePropertiesFile () { + // Trace message + this.getLogger().trace("CALLED!"); //NOI18N + try { // Write it - this.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); + this.properties.store(new PrintWriter(FrameworkInterface.PROPERTIES_CONFIG_FILE), "This file is automatically generated. You may wish to alter it."); //NOI18N } catch (final IOException ex) { this.abortProgramWithException(ex); } + + // Trace message + this.getLogger().trace("EXIT!"); //NOI18N } /** diff --git a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java index f2fea93..6a7b009 100644 --- a/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java +++ b/Addressbook/src/org/mxchange/addressbook/database/backend/mysql/MySqlDatabaseBackend.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.text.MessageFormat; import java.util.Iterator; import org.mxchange.addressbook.contact.Contact; import org.mxchange.addressbook.database.backend.BaseDatabaseBackend; @@ -49,16 +50,26 @@ public class MySqlDatabaseBackend extends BaseDatabaseBackend implements Databas // Validate driver this.validateDriver("mysql"); //NOI18N - // Now that the driver is there ... + // Now that the driver is there, set the table name + this.setTableName(tableName); } @Override public void connectToDatabase () throws SQLException { + // Trace message + this.getLogger().trace("CALLED!"); + // Generate connection string - String connect = "jdbc:mysql://" + this.getProperty("org.mxchange.database.mysql.host") + "/" + this.getTableName(); + String connect = String.format("jdbc:mysql://%s/%s", this.getProperty("org.mxchange.addressbook.database.mysql.host"), this.getProperty("org.mxchange.addressbook.database.mysql.dbname")); + + // Debug message + this.getLogger().debug(MessageFormat.format("Attempting to connect to {0} ...", connect)); // Now get a connection instance back - this.connection = DriverManager.getConnection(connect, this.getProperty("org.mxchange.database.mysql.login"), this.getProperty("org.mxchange.database.mysql.password")); + this.connection = DriverManager.getConnection(connect, this.getProperty("org.mxchange.addressbook.database.mysql.login"), this.getProperty("org.mxchange.addressbook.database.mysql.password")); + + // Trace message + this.getLogger().trace("EXIT!"); } @Override