mysql quick reference

Sunday, January 25, 2009

Launch mysqld (server) on Linux (if not already running):
sudo /etc/init.d/mysql start

Connect to server [specific database]:
mysql [-h host] -u user -p [dbname to use]

Use the SHOW statement to find out what databases currently exist on the server:
mysql> SHOW DATABASES;

Pick one:
mysql> USE dbname

Create new one:
mysql> CREATE DATABASE dbname;

List tables:
mysql> SHOW TABLES;

Create table:
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

List tables columns:
mysql> DESCRIBE tablename;

Generate schema:
mysql> SHOW CREATE TABLE tablename;

use jndi with spring to access external properties file

Tuesday, January 13, 2009

On the surface, external configuration files in a JEE environment provide a simple mechanism to store environment-specific data, but they can be a pain to access. How to access it when file system access isn't allowed? Since it's outside the classpath, where is the file in each environment? If I'm using Spring, how do I access this movable file?

The best solution I've found is to use jndi resources. The following is a solution using Websphere (6) and Spring (2.01).

Step 1: Configure the jndi reference for Websphere
This step was based on information from IBM's page "Using URL resources to manage J2EE property files in IBM WebSphere Application Server V5", steps A and B. Websphere Studio isn't required, however, so briefly:
a) Navigate to Resources > URL > URLs and create a new URL. Make up a JNDI name starting with "url/". In 'specification', enter the path to the properties file as a URI, eg. "file:///E:/project.properties". So now Websphere has a URL Resource pointing to the properties file for this environment.
b) In the code, edit web.xml. Configure a new resource-ref as shown in IBM's figure 6. 'res-ref-name' is the jndi name we set up in a). Then in ibm-web-bnd.xmi, add a new resRefBindings as shown in IBM's figure 7.
That completes the configuration of Websphere and the jndi configuration in the code.

Step 2: Next Spring needs to be able to load the properties file by looking up the jndi location.
c) I'll assume that you want the properties file to be set as a property on a class 'pkg.MyClass'. To do this, we use a PropertyFactoryBean to convert from properties file to Properties class. The PropertyFactoryBean takes a Resource as location property, so we create a UrlResource bean for this, with the java.net.Url as constructor argument. This java.net.Url is the result of using a JndiObjectFactoryBean to look up the jndi name and return the Url object. The following bean config shows these conversions:


<bean class="myclass">
 <property name="props">
  <!-- Load from the .properties file-->
  <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
   <property name="location">
    <!-- Generate a UrlResource from the java.net.Url -->
    <bean class="org.springframework.core.io.UrlResource">
     <constructor-arg>
      <!-- use jndi to look up the location of the parameters.properties file -->
      <bean class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName" value="java:comp/env/url/analysisParametersURL" />
      </bean>
     </constructor-arg>
    </bean>
   </property>
  </bean>
 </property>
</bean>


And that's it. In summary, Spring looks up a jndi URL reference to a properties file, configured in the JEE server. Spring beans are created that convert the URL to a URLResource to a Properties object, available for injection into your custom class.