hps-java/src/main/java/org/lcsim/hps/conditions
diff -N ConnectionParameters.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ConnectionParameters.java 18 Sep 2013 01:13:32 -0000 1.1
@@ -0,0 +1,63 @@
+package org.lcsim.hps.conditions;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+/**
+ * This class encapsulates the parameters for connecting to a database,
+ * including hostname, port, user and password. It can also create and
+ * return a Connection object based on these parameters.
+ *
+ * @author jeremym
+ * @version $Id: ConnectionParameters.java,v 1.1 2013/09/18 01:13:32 jeremy Exp $
+ */
+public final class ConnectionParameters {
+
+ /* Default values for the MySQL database at SLAC. */
+ private String user = "rd_hps_cond_ro";
+ private String password = "2jumpinphotons.";
+ private int port = 3306;
+ private String hostname = "mysql-node03.slac.stanford.edu";
+
+ ConnectionParameters() {
+ }
+
+ ConnectionParameters(String user, String password, String hostname, int port) {
+ this.user = user;
+ this.password = password;
+ this.hostname = hostname;
+ this.port = port;
+ }
+
+ Properties getConnectionProperties() {
+ Properties p = new Properties();
+ p.put("user", user);
+ p.put("password", password);
+ return p;
+ }
+
+ String getHostname() {
+ return hostname;
+ }
+
+ int getPort() {
+ return port;
+ }
+
+ String getConnectionString() {
+ return "jdbc:mysql://" + hostname + ":" + port + "/";
+ }
+
+ Connection createConnection() {
+ Properties connectionProperties = getConnectionProperties();
+ Connection connection = null;
+ try {
+ connection = DriverManager.getConnection(getConnectionString(), connectionProperties);
+ } catch (SQLException x) {
+ throw new RuntimeException("Failed to connect to database: " + getConnectionString(), x);
+ }
+ return connection;
+ }
+}
\ No newline at end of file