Intro - Part 1 - Part 2 - Part 3 - Part 4

Now we are going to setup an Oracle database and see what needs to be changed so that it replaces the Postgresql Database.

Create the user in your Oracle database

Connect as 'system' user to your oracle database via sqlplus, and create the 'todo' user.

sqlplus system@YOURDB

(swap YOURDB for the name of your database instance)

Issue the following commands:

CREATE USER todo IDENTIFIED BY todo DEFAULT TABLESPACE users TEMPORARY TABLESPACE TEMP QUOTA UNLIMITED ON users;

GRANT CONNECT, RESOURCE TO todo;

Now create the tables, insert data and install the function

Connect as your newly created user to the oracle database and execute the following sql commands.

sqlplus todo/todo

Create the table..

create table t_todo ( todo_id int primary key,  description varchar(128), rank int ) ;

create sequence seq_todo increment by 1 start with 5000;

Insert the data ..

insert into t_todo (todo_id, description, rank)   
values (seq_todo.nextval,'clean watertank',1);

insert into t_todo (todo_id, description, rank)  
values (seq_todo.nextval,'mow lawn',2);

insert into t_todo (todo_id, description, rank)  
values (seq_todo.nextval, 'clean bicycle',3);

insert into t_todo (todo_id, description, rank)  
values (seq_todo.nextval, 'take out garbage',4);

Install the following function :

create or replace
function add_new_task( a_task_description varchar2 ) return numeric
as
    l_next_seq_value numeric;
    l_max_rank numeric;
begin

    --  get the next sequence Value
    l_next_seq_value:=seq_todo.nextval;

    -- suss out the max rank
    select max(rank)
    into   l_max_rank
    from t_todo;


    -- and insert 
    insert into t_todo(todo_id, description, rank)
    values(l_next_seq_value, a_task_description, l_max_rank+1);

    return l_next_seq_value;
end add_new_task;
/

We're done with Oracle.

Change the code

Open up Java class myapp.server.common.BaseService in Eclipse and change the 'getConnection()' code: so that it looks like this:

 public Connection getConnection()
      throws SQLException, ApplicationException
      {

          if (connection==null)
          {
              String database = "jdbc:oracle:thin:@localhost:1521:YOURDB";
              String username = "todo";
              String password = "todo";

              // Load the Oracle JDBC driver
              DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

              // Connect to the database
              connection =
                  DriverManager.getConnection ( database, username, password );

              if (connection==null) throw new ApplicationException("Could not acquire connection!");
          }
          return connection;
      }

Note: you most likely need to change the YOURDB into the name of your database.

Here's the complete BaseService.java file.

Change the target in the .mlg file

Open the Todo.mlg file and change target postgresql into oracle:

int addNewTask(String task) target postgresql  

should now look like this:

int addNewTask(String task) target oracle

Here's the whole Todo.mlg file.

Let amalegeni regenerate the Java code

cd ~/workspace/Todo
amalegeni -t template/gwt.stg src

Output:

Using template: gwt.stg
Handling file: /home/willem/workspace/Todo/src/myapp/client/Todo.mlg
Writing to file: src/myapp/client/generated/TodoBean.java
Writing to file: src/myapp/client/generated/TodoService.java
Writing to file: src/myapp/client/generated/TodoServiceAsync.java
Writing to file: src/myapp/server/generated/TodoServiceImpl.java
Writing to file: src/myapp/client/generated/TodoService-cut_and_paste.txt

Now refresh the code in Eclipse (click F5 on highlighting the 'src' directory).

Add the oracle JDBC to the build path

Highlight project 'Todo' in the package explorer, click Properties, and select 'Java Build Path'. Click on the tab 'Libraries', and here you add the Oracle JDBC jar file, I'm using this one: ojdbc6.jar

Run that app

Launch the app.

© Willem Moors, 2009 - 2013