JDBCBaseDataFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 package org.openmobileis.examples.server.database;
00026 
00027 import java.sql.Connection;
00028 import java.sql.DatabaseMetaData;
00029 import java.sql.ResultSet;
00030 
00031 import org.openmobileis.common.util.database.AbstractQueryManager;
00032 import org.openmobileis.common.util.exception.DatabaseException;
00033 
00041 public final class JDBCBaseDataFactory extends AbstractQueryManager {
00042 
00043   private static String queryUpdate  =
00044     "UPDATE BASEDATA SET DATA='%0%' " +
00045     "WHERE ID='%1%'";
00046 
00047    private static String queryCreate  =
00048      "INSERT INTO BASEDATA (ID , DATA) " +
00049       "VALUES ('%0%', '%1%')";
00050 
00051   // query pattern used to create syncNB table
00052   private static String queryCreateTable  =
00053     "CREATE TABLE BASEDATA ("
00054     +" ID varchar(30) NOT NULL,"
00055     +" DATA varchar(50),"
00056     +" CONSTRAINT dataid PRIMARY KEY (ID)"
00057     +")";
00058 
00059   private static String queryDelete =
00060     "DELETE FROM BASEDATA WHERE ID = '%0%'";
00061 
00062 
00063   /********  SELECT QUERIES ********************/
00064 
00065   private static String queryGetBaseData  =
00066     "SELECT ID, DATA  FROM BASEDATA WHERE ID = '%0%' ";
00067 
00068   private static String queryGetAllBaseData  =
00069     "SELECT ID, DATA  FROM BASEDATA ";
00070 
00074   public JDBCBaseDataFactory() {
00075     super();
00076   }
00077 
00078   /*
00079   * check if the sync table exists. IF it does not exist, create it
00080   */
00081   public void createSyncTable() throws DatabaseException, java.sql.SQLException {
00082     try {
00083       Connection con = this.getDbManager().getConnection();
00084       DatabaseMetaData dbmd = con.getMetaData();
00085       String[] tableType = {"TABLE"};
00086       ResultSet result = dbmd.getTables (null,null,"BASEDATA", tableType);
00087       if (!result.next()) {
00088         this.executeUpdate(queryCreateTable, null);
00089       }
00090     } finally {
00091       this.getDbManager().garbageOpenedConnection();
00092     }
00093   }
00094 
00095   public void create (String parameters[]) throws DatabaseException {
00096     this.executeUpdate(queryCreate, parameters);
00097   }
00098 
00099  public void delete (String parameters[]) throws DatabaseException {
00100     this.executeUpdate(queryDelete, parameters);
00101  }
00102 
00103  public void update (String parameters[]) throws DatabaseException {
00104     this.executeUpdate(queryUpdate, parameters);
00105  }
00106 
00107  public ResultSet getBaseData (String parameters[]) throws DatabaseException {
00108     return (this.executeQuery(queryGetBaseData, parameters));
00109   }
00110 
00111  public ResultSet getAllBaseData () throws DatabaseException {
00112     return (this.executeQuery(queryGetAllBaseData, null));
00113   }
00114 
00115 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1