Esempio di codice: creazione di un inserimento con parametri

/*
* ParameterizedInsert.java
*
* Research In Motion Limited proprietary and confidential
* Copyright Research In Motion Limited, 2010
*/
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.database.*;
import net.rim.device.api.io.*;
import java.util.*;
public class ParameterizedInsert extends UiApplication
{
public static void main(String[] args)
{
   ParameterizedInsert theApp = new ParameterizedInsert();
   theApp.enterEventDispatcher();
}
public ParameterizedInsert()
{
   pushScreen(new ParameterizedInsertScreen());
}
}
class ParameterizedInsertScreen extends MainScreen
{
   Database d;
   public ParameterizedInsertScreen()
   {
      LabelField title = new LabelField("SQLite Insert Data " +
      "Schema Sample",
      LabelField.ELLIPSIS |
      LabelField.USE_ALL_WIDTH);
      setTitle(title);
      add(new RichTextField("Attempting to insert data into " +
      "MyTestDatabase.db on the SDCard."));
    try
    {
       URI myURI = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
       "MyTestDatabase.db");
       d = DatabaseFactory.open(myURI);
       Statement st = d.createStatement("INSERT INTO People(Name,Age) " +
       "VALUES (?,?)");
       st.prepare();
       Hashtable ht = new Hashtable(4);
       ht.put("Sophie", new Integer(6));
       ht.put("Karen",  new Integer(3));
       ht.put("Kevin",  new Integer(82));
       ht.put("Cindy",  new Integer(12));
       Enumeration names = ht.keys();
       Enumeration ages  = ht.elements();
       while (names.hasMoreElements())
       {
          String strName = (String)names.nextElement();
          Integer iAge   = (Integer)ages.nextElement();
          st.bind(1,strName);
          st.bind(2,iAge.intValue());
          st.execute();
          st.reset();
       }
       st.close();
       d.close();
    }
    catch ( Exception e )
    {
       System.out.println( e.getMessage() );
       e.printStackTrace();
    }
}
}

Le informazioni sono state utili? Inviateci i vostri commenti.