mysql
Class SensorSql

java.lang.Object
  extended by util.PropertyContainer
      extended by util.PropertyResources
          extended by util.PropertyBundles
              extended by mysql.SensorSql
All Implemented Interfaces:
Cloneable, Initializable, LocalizedSupplying, PropertySupplying, ResourceSupplying

public class SensorSql
extends PropertyBundles
implements Initializable

A class that provides data base access to weather sensor data. It allows inserting and retrieval of a DataEvents from a postgresql database.


Nested Class Summary
 
Nested classes/interfaces inherited from class util.PropertyResources
PropertyResources.URLResource
 
Field Summary
private  Connection db
          The connection to the database.
private static String DEFBRIDGE
          The default JDBC-postgres bridge class.
private static String DEFCONNECT
          The default driver-specific connection string.
private static SimpleDateFormat DEFDATEFORMAT
          The default date format .
private static String DEFPASSWORD
          The default password used for identifying the user.
private static String DEFTABLE
          The default name of the table to use in database queries.
private static String DEFUSER
          The default user name to identify the class on the database.
private  Statement execute
          The executional statement.
static String KEY_BRIDGE
          The class name of the JDBC-postgres bridge.
static String KEY_CONNECT
          The driver-specific connection string.
static String KEY_DATEFORMAT
          The date formatter to use.
static String KEY_PASSWORD
          The password used for identifying the user.
static String KEY_TABLE
          The name of the table to use in database queries.
static String KEY_USER
          The user name to identify the class on the database.
private static boolean LOADED
          True, if driver class has been loaded to avoid overhead.
 
Fields inherited from class util.PropertyBundles
KEY_LOCALECOUNTRY, KEY_LOCALELANGUAGE, KEY_RESOURCEBUNDLES
 
Fields inherited from class util.PropertyResources
KEY_NOINITONCREATE, localurl, locate, POSTFIX_DIR, POSTFIX_EXT, POSTFIX_FILE, POSTFIX_LIST, POSTFIX_URL, urlset
 
Fields inherited from class util.PropertyContainer
KEY_LISTSEPARATOR, KEY_MAPKEYVALUECHAR, KEY_MAPSEPARATOR
 
Fields inherited from interface util.ResourceSupplying
KEY_URLRESOURCES, KEY_URLUSECONFIG, KEY_URLUSECURRENT, KEY_URLUSEHOME
 
Fields inherited from interface util.PropertySupplying
CONFIG, KEY_CLASS
 
Constructor Summary
SensorSql(Map prop)
          Constructs a new sql-db access object.
 
Method Summary
 void close()
          Closes the sensor database.
private  void closeDatabase()
          Closes the connection.
private  void closeStatement()
          Closes the statement.
private  void createStatement()
          Creates the executable statement.
private  String getDatabaseEntry(Object val)
          Formats the argument to allow it to be entered into a normal database.
 void init()
          Initializes the sensor-data to SQL-DB access.
 void insert(Map data)
          Inserts new data into the database.
 void open()
          Opens the sensor database.
private  void openDatabase()
          Opens the connection to the database.
 void replace(Map data, String where)
          Replaces data in the database.
 ResultSet retrieve(List fields, String where)
          Returns the result of a database query.
 
Methods inherited from class util.PropertyBundles
clone, getLocalized, getLocalized, getLocalizedString, getLocalizedString, loadResource
 
Methods inherited from class util.PropertyResources
createFrom, createFrom, createFrom, getApplet, getAsResources, getLocalClassLoader, getPropertiesToKey, getPropertiesToKey, getResource, getResourceAsStream, getResourceFromKey, getResources, keyCreate, keyCreate, reload, setApplet
 
Methods inherited from class util.PropertyContainer
augment, augment, augment, defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsEnums, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, isNew, parseObject, reload, removeProperty, rescanned, setObject, setProperties, setProperty, stringProperties, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface util.ResourceSupplying
getResource, getResourceAsStream, getResources
 
Methods inherited from interface util.PropertySupplying
defaultBoolean, defaultChar, defaultDouble, defaultFloat, defaultInt, defaultLong, defaultObject, defaultObject, defaultProperties, defaultProperty, getAsBoolean, getAsChar, getAsDouble, getAsFloat, getAsInt, getAsList, getAsLong, getAsMap, getAsObject, getAsObject, getProperties, getProperty, has, parseObject, removeProperty, setObject, setProperty, stringProperties
 

Field Detail

KEY_BRIDGE

public static final String KEY_BRIDGE
The class name of the JDBC-postgres bridge.

See Also:
Constant Field Values

KEY_CONNECT

public static final String KEY_CONNECT
The driver-specific connection string.

See Also:
Constant Field Values

KEY_USER

public static final String KEY_USER
The user name to identify the class on the database.

See Also:
Constant Field Values

KEY_PASSWORD

public static final String KEY_PASSWORD
The password used for identifying the user. Not encrypted.

See Also:
Constant Field Values

KEY_TABLE

public static final String KEY_TABLE
The name of the table to use in database queries.

See Also:
Constant Field Values

KEY_DATEFORMAT

public static final String KEY_DATEFORMAT
The date formatter to use.

See Also:
Constant Field Values

DEFBRIDGE

private static final String DEFBRIDGE
The default JDBC-postgres bridge class. Once loaded, never used.

See Also:
Constant Field Values

DEFCONNECT

private static final String DEFCONNECT
The default driver-specific connection string.

See Also:
Constant Field Values

DEFUSER

private static final String DEFUSER
The default user name to identify the class on the database.

See Also:
Constant Field Values

DEFPASSWORD

private static final String DEFPASSWORD
The default password used for identifying the user. Not encrypted.

See Also:
Constant Field Values

DEFTABLE

private static final String DEFTABLE
The default name of the table to use in database queries.

See Also:
Constant Field Values

DEFDATEFORMAT

private static final SimpleDateFormat DEFDATEFORMAT
The default date format .


LOADED

private static boolean LOADED
True, if driver class has been loaded to avoid overhead.


db

private Connection db
The connection to the database.


execute

private Statement execute
The executional statement.

Constructor Detail

SensorSql

public SensorSql(Map prop)
Constructs a new sql-db access object. All keys present have default values. The database connection is opened when the first call to the init() method occurs.

Method Detail

init

public void init()
Initializes the sensor-data to SQL-DB access. On a first call to this method, the JDBC-driver class, KEY_BRIDGE, is loaded.

Specified by:
init in interface Initializable
Overrides:
init in class PropertyResources

open

public void open()
          throws SQLException
Opens the sensor database. The connection is established and the executable statement is constructed.

Throws:
SQLException

close

public void close()
           throws SQLException
Closes the sensor database. First, the statement is closed, then the connection to the database itself is closed.

Throws:
SQLException

insert

public void insert(Map data)
            throws SQLException
Inserts new data into the database. The table used is defined in the underlying properties. The string that is executed reads as
       INSERT INTO {table} ({field1}, {field2},...) VALUES({num1}, '{string2}',...)
       
The user supplied lists are the field list and the values list. The values should consist of object of types String, Numbers, or Dates. String are converted to be embraced with single quotes ('), Date objects are converted into strings using the KEY_DATEFORMAT format. Numbers are simply converted into strings. Note that it is the users responsibility to ensure proper ordering in the list, as no further type checks are done, only the run-time types of the values are used.

Parameters:
fields - A list of strings giving the field names in the database
values - A list of objects that are the correspondend values to the fields in the parameter above.
Throws:
SQLException

replace

public void replace(Map data,
                    String where)
             throws SQLException
Replaces data in the database. The table used is defined in the underlying properties. The string that is executed reads as
       UPDATE {table} SET {field1}={num1}, {field2}='{string2}',...
       
The user supplied lists are the field list and the values list. The values should consist of object of types String, Numbers, or Dates. String are converted to be embraced with single quotes ('), Date objects are converted into strings using the KEY_DATEFORMAT format. Numbers are simply converted into strings. Note that it is the users responsibility to ensure proper ordering in the list, as no further type checks are done, only the run-time types of the values are used.

Parameters:
fields - A list of strings giving the field names in the database
values - A list of objects that are the correspondend values to the fields in the parameter above.
where - The identification which sets should be affected, without the leading WHERE.
Throws:
SQLException

retrieve

public ResultSet retrieve(List fields,
                          String where)
                   throws SQLException
Returns the result of a database query. The field names supplied in the first name are replaced with an asterisk if the list is null or empty. The where clause should contain all necessary information, without the laeding WHERE.

Parameters:
fields - A list of fields to retrieve or null, if all fields are needed.
where - The WHERE clause without the leading WHERE
Throws:
SQLException

getDatabaseEntry

private String getDatabaseEntry(Object val)
Formats the argument to allow it to be entered into a normal database. Right now, the following objects are recognized:


openDatabase

private void openDatabase()
                   throws SQLException
Opens the connection to the database.

Throws:
SQLException

createStatement

private void createStatement()
                      throws SQLException
Creates the executable statement.

Throws:
SQLException

closeStatement

private void closeStatement()
                     throws SQLException
Closes the statement.

Throws:
SQLException

closeDatabase

private void closeDatabase()
                    throws SQLException
Closes the connection.

Throws:
SQLException