Getting started with the examples

Using postgresql

The examples are targeted at the postgresql database, if you want to use mysql then looked further down this page. Only examples which use plain sql will work with Mysql, since the procedural sql (pl-sql) in Postgresql is different (and a bit more advanced)...

Assumptions

  • no need for an IDE
  • you do need to have amalegeni installed
  • and of course you are assumed to have ant, java, postgres already installed and running

Ant build file

This is the ant build file used in most, if not all, examples.

Task 'createtemplate' creates the amalegeni cs (client/server) template if it doesn't exist yet.


  <?xml version="1.0" encoding="utf-8" ?>
  <project name="examples" default="compile" basedir=".">
  
    <path id="classpath">
      <pathelement location="."/>
      <pathelement path="/opt/amalegeni/amalegeni.jar" />
      <pathelement path="/opt/jdbc/postgres/postgresql-8.4-701.jdbc4.jar"/>
    </path>
  
    <target name="createtemplate" depends="template.check" unless="template.exists" 
            description="Create the template">
      <java classname="amalegeni.Main" classpathref="classpath">
        <arg value="-s"/><arg value="cs.stg"/>
        <arg value="-o"/><arg value="cs.stg"/>
      </java>
    </target>
  
    <target name="template.check">
      <condition property="template.exists"><available file="cs.stg"/>
      </condition>
    </target>
  
    <target name="amalegeni" depends="createtemplate" 
            description="Generate code from amalegeni bean/service definition">
      <java classname="amalegeni.Main" classpathref="classpath">
        <arg value="-t"/><arg value="cs.stg"/> <!-- the template file to use -->
        <arg value="."/> <!-- start looking for .mlg files from within the this directory -->
      </java>
    </target>
  
    <target name="compile" depends="amalegeni" description="Compile java source">
      <javac srcdir="." includes="**" encoding="utf-8" destdir=".">
        <classpath refid="classpath"/>
      </javac>
    </target>
  
    <target name="run" depends="compile" description="Run compiled java classes">
      <java classname="Main" classpathref="classpath">
      </java>
    </target>
  
  </project>

Setup the database user

The database user 'todo' that was introduced in GWT Examples is used for these examples (except when explicitly stated differently). If you haven't yet, then you can create it this way in postgresql:

$ sudo -u postgres psql template1
# create user todo encrypted password 'todo' ;
# create database todo with encoding 'UNICODE' owner todo;
# \q

Using mysql

Create the 'todo' user.

  mysqladmin -u root -p create todo 
  
  mysql -u root -p
  mysql> use todo;
  mysql> grant all on todo to 'todo'@'localhost' identified by 'todo';
  mysql> grant  SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER on todo.* to  'todo'@'localhost' 
  mysql> flush privileges;
  mysql> \q

Modify the BaseSevice

Change the getConnection() method in the class common.BaseService to read as follows:

    public Connection getConnection()
    throws SQLException
    {
        if (connection==null)
        {
            try
            {
                String database = "todo";
                String username = "todo";
                String password = "todo";

                Class.forName("com.mysql.jdbc.Driver").newInstance();

                connection = DriverManager.getConnection("jdbc:mysql://localhost/" + database
                                + "?user="+username+"&password="+password
                            );
            }
            catch( IllegalAccessException iae)
            {
                System.err.println("IllegalAccessException: " + iae.getMessage());
            }
            catch( InstantiationException ie)
            {
                System.err.println("InstantiationException: " + ie.getMessage());
            }
            catch( ClassNotFoundException cnfe)
            {
                System.err.println("ClassNotFoundException: " + cnfe.getMessage());
            }
            if (connection==null) throw new SQLException("Could not acquire connection!");
            }
          return connection;
      }

Adapt the build.xml

Change the 'classpath' to include a line that refers to the mysql jdbc jar file :

    <pathelement path="/opt/jdbc/mysql/mysql-connector-java-5.1.10-bin.jar"/>

The line containing the reference to the postgresql jdbc jar may not be needed anymore, and can be removed.

© Willem Moors, 2009 - 2013