From f1f53140fa1f2bc46c4576f953a8634dd8a5cb22 Mon Sep 17 00:00:00 2001 From: goik <goik@hdm-stuttgart.de> Date: Sat, 20 Oct 2012 13:04:23 +0200 Subject: [PATCH] Database connection parameters as Java properties. Javadoc PersistenceHandler class completetd --- .../Jdbc/src/sda/jdbc/intro/v1/DbProps.java | 22 ++++ .../sda/jdbc/intro/v1/PersistenceHandler.java | 104 ++++++++++++------ .../intro/v1/UsagePersistenceHandler.java | 18 +++ .../src/sda/jdbc/intro/v1/database.properties | 3 + 4 files changed, 114 insertions(+), 33 deletions(-) create mode 100644 ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/DbProps.java create mode 100644 ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/UsagePersistenceHandler.java create mode 100644 ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/database.properties diff --git a/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/DbProps.java b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/DbProps.java new file mode 100644 index 000000000..0bea232e0 --- /dev/null +++ b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/DbProps.java @@ -0,0 +1,22 @@ +package sda.jdbc.intro.v1; + +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +public class DbProps { + private static final String BUNDLE_NAME = "sda.jdbc.intro.v1.database"; + + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle + .getBundle(BUNDLE_NAME); + + private DbProps() { + } + + public static String getString(String key) { + try { + return RESOURCE_BUNDLE.getString(key); + } catch (MissingResourceException e) { + return '!' + key + '!'; + } + } +} diff --git a/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/PersistenceHandler.java b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/PersistenceHandler.java index 700dec67f..a07f4e2bf 100644 --- a/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/PersistenceHandler.java +++ b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/PersistenceHandler.java @@ -5,13 +5,25 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; - /** - * @author goik + * Handle database communication. There are two + * distinct internal states <q>disconnected</q> and <q>connected</q>, see + * {@link #isConnected()}. These two states may be toggled by invoking + * {@link #connect()} and {@link #disconnect()} respectively. * - * This class is intended to handle all database communication. There are two - * distinct internal states "disconnected" and "connected", see - * {@link #isConnected()} + * The following snippet illustrates the intended usage: + * <pre> public static void main(String[] args) { + final PersistenceHandler ph = new PersistenceHandler(); + if (ph.connect()) { + if (!ph.add("Jim", "jim@foo.com")) { + System.err.println("Insert Error:" + ph.getErrorMessage()); + } + } else { + System.err.println("Connect error:" + ph.getErrorMessage()); + } + }</pre> + * + * @author goik */ public class PersistenceHandler { @@ -19,20 +31,31 @@ public class PersistenceHandler { Statement stmt = null; String errorMessage = null; - + /** - * <dt><b>Precondition:</b><dd> - * must be in "connected" state, see {@link #isConnected()}} - * - * Inserting a (name,email) record into the database server - * @param name - * @param email - * @return true if the current data record has been successfully - * inserted into the database server. + * Instance in <q>disconnected</q> state. See {@link #isConnected()} */ + public PersistenceHandler() {/* only present here to supply javadoc comment */} + + /** + * Inserting a (name, email) record into the database server. In case of + * errors corresponding messages may subsequently be retrieved by calling + * {@link #getErrorMessage()}. + * + * <dt><b>Precondition:</b></dt> <dd>must be in + * <q>connected</q> state, see {@link #isConnected()}</dd> + * + * @param name + * A person's name + * @param email + * A person's email address + * + * @return true if the current data record has been successfully inserted + * into the database server. false in case of error(s). + */ public boolean add(final String name, final String email){ - final String sql = "INSERT INTO Person VALUES('" + name + "', '" + - email + "')"; + final String sql = "INSERT INTO Person VALUES('" + name + "', '" + + email + "')"; try { stmt.executeUpdate(sql); return true; @@ -42,33 +65,43 @@ public class PersistenceHandler { } } - /** - * If a call to {@link #add(String, String)}, {@link #connect()}, - * or {@link #disconnect()} returns false, the corresponding error explanation - * my be retrieved - * @return the error explanation corresponding to the latest failed operation, - * null otherwise - */ + /** + * Retrieving error messages in case a call to {@link #add(String, String)}, + * {@link #connect()}, or {@link #disconnect()} yields an error. + * + * @return the error explanation corresponding to the latest failed + * operation, null if no error yet occurred. + */ public String getErrorMessage() { return errorMessage; } /** - * Open a connection to a database server - * <dt><b>Precondition:</b><dd> - * must be in disconnected state, see {@link #isConnected()} + * Open a connection to a database server. + * + * <dt><b>Precondition:</b><dd> + * <dd>must be in <q>disconnected</q> state, see {@link #isConnected()}</dd> + * + * <dt><b>Precondition:</b><dd> + * <dd>The following properties must be set: + * <pre>PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm +PersistenceHandler.password=XYZ +PersistenceHandler.username=foo</pre> + * </dd> * * @return true if connecting was successful */ public boolean connect () { try { conn = DriverManager.getConnection( - "jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ"); + DbProps.getString("PersistenceHandler.jdbcUrl"), + DbProps.getString("PersistenceHandler.username"), + DbProps.getString("PersistenceHandler.password")); try { stmt = conn.createStatement(); return true; } catch (SQLException e) { - errorMessage = "Connection opened but Statement creation failed:\"" + e.getMessage() + "\"."; + errorMessage = "Connection opened but Statement creation failed:\"" + e.getMessage() + "\"."; try { conn.close(); } catch (SQLException ee) { @@ -82,12 +115,17 @@ public class PersistenceHandler { } return false; } + /** - * Open a connection to a database server - * <dt><b>Precondition:</b><dd> - * must be in "connected" state, see {@link #isConnected()} + * Close a connection to a database server and clean up JDBC related resources + * + * Error messages in case of failure may subsequently be retrieved by + * calling {@link #getErrorMessage()}. + * + * <dt><b>Precondition:</b></dt> + * <dd>must be in <q>connected</q> state, see {@link #isConnected()}</dd> * - * @return true if disconnecting was successful + * @return true if disconnecting was successful, false in case error(s) occur. */ public boolean disconnect() { boolean resultStatus = true; @@ -113,7 +151,7 @@ public class PersistenceHandler { } /** - * An instance can either be in "connected" or "disconnected" state. The + * An instance can either be in <q>connected</q> or <q>disconnected</q> state. The * state can be toggled by invoking {@link #connect()} or * {@link #disconnect()} respectively. * diff --git a/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/UsagePersistenceHandler.java b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/UsagePersistenceHandler.java new file mode 100644 index 000000000..ebdf5c95a --- /dev/null +++ b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/UsagePersistenceHandler.java @@ -0,0 +1,18 @@ +package sda.jdbc.intro.v1; + +public class UsagePersistenceHandler { + + /** + * @param args + */ + public static void main(String[] args) { + final PersistenceHandler ph = new PersistenceHandler(); + if (ph.connect()) { + if (!ph.add("Jim", "jim@foo.com")) { + System.err.println("Insert Error:" + ph.getErrorMessage()); + } + } else { + System.err.println("Connect error:" + ph.getErrorMessage()); + } + } +} diff --git a/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/database.properties b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/database.properties new file mode 100644 index 000000000..32c844bac --- /dev/null +++ b/ws/eclipse/Jdbc/src/sda/jdbc/intro/v1/database.properties @@ -0,0 +1,3 @@ +PersistenceHandler.jdbcUrl=jdbc:mysql://localhost:3306/hdm +PersistenceHandler.password=XYZ +PersistenceHandler.username=hdmuser -- GitLab