Skip to content
Snippets Groups Projects
Commit ae0cf7a5 authored by Goik Martin's avatar Goik Martin
Browse files

Externalize JDBC parameters to properties file. Adjust javadoc.

parent 22e35dd9
No related branches found
No related tags found
No related merge requests found
package sda.jdbc.intro;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* A "getting started" example to insert data into a table by
* using JDBC. No error handling whatsoever is supplied!
*
* @author goik
*/
public class SimpleInsert {
/**
* User credentials (name and password) are supplied within code. This is
* absolutely uuuuuuugly! For a Java property based solution
* see {@link sda.jdbc.intro.v1.SimpleInsert}.
*
* @param args unused
* @throws SQLException possible causes:
* <ul>
* <li>Driver matching JDBC URL unavailable</li>
* <li>No network access to database server</li>
* <li>Invalid credentials</li>
* <li>Database table 'Person' does not exist</li>
* <li>duplicate email (UNIQUE key)</li>
* <li>... something forgotten?</li>
* </ul>
*/
public static void main(String[] args) throws SQLException {
// Step 1: Open a connection to the database server
final Connection conn = DriverManager.getConnection(
......
package sda.jdbc.intro.v1;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
/**
* Originally generated from {@link sda.jdbc.intro.SimpleInsert} by using
* Eclipse's "Externalize Strings" function.+
*
* @author goik
*/
public class JdbcProperties {
private static final String BUNDLE_NAME = "sda.jdbc.intro.v1.jdbc";
private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
.getBundle(BUNDLE_NAME);
private JdbcProperties() {}
public static String getString(String key) {
try {
return RESOURCE_BUNDLE.getString(key);
} catch (MissingResourceException e) {
return '!' + key + '!';
}
}
}
......@@ -5,18 +5,39 @@ import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* A "getting started" example to insert data into a table by
* using JDBC. No error handling whatsoever is supplied!
*
* In contrast to {@link sda.jdbc.intro.SimpleInsert} JDBC related
* properties have been externalized to file <q>database.properties</q>.
*
* @author goik
*/
public class SimpleInsert {
/**
* @param args unused
* @throws SQLException possible causes:
* <ul>
* <li>Driver matching JDBC URL unavailable</li>
* <li>No network access to database server</li>
* <li>Invalid credentials</li>
* <li>Database table 'Person' does not exist</li>
* <li>duplicate email (UNIQUE key)</li>
* <li>... something forgotten?</li>
* </ul>
*/
public static void main(String[] args) throws SQLException {
// Step 1: Open a connection to the database server
final Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/hdm", "hdmuser", "XYZ");
JdbcProperties.getString("SimpleInsert.jdbcUrl"), JdbcProperties.getString("SimpleInsert.username"), JdbcProperties.getString("SimpleInsert.password")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// Step 2: Create a Statement instance
final Statement stmt = conn.createStatement();
// Step 3: Execute the desired INSERT
final int updateCount = stmt.executeUpdate(
"INSERT INTO Person VALUES('Jim', 'jim@foo.org')");
"INSERT INTO Person VALUES('Jim', 'jim@foo.org')"); //$NON-NLS-1$
// Step 4: Give feedback to the enduser
System.out.println("Successfully inserted " + updateCount + " dataset(s)");
System.out.println("Successfully inserted " + updateCount + " dataset(s)"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ public class UsagePersistenceHandler {
/**
* creates a {@link PersistenceHandler} instance, tries to connect
* to a database and insert a data set ('Jim', 'jim@foo.com') to
* the relation Person.
* the existing table Person.
* @param args unused
*/
public static void main(String[] args) {
......
SimpleInsert.jdbcUrl=jdbc:mysql://localhost:3306/hdm
SimpleInsert.password=XYZ
SimpleInsert.username=hdmuser
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment