Friday, August 1, 2014

Retrieving Cloud SQL records to Android App over App Engine

The "Generate Endpoint Class" builds code for your App Engine to insert, update, remove and list your entities stored in the Cloud Datastore. I was out of luck to find any samples or functions to build code to connect and store my data in Cloud SQL. And so, I modified those generated code for connecting my Cloud SQL instance instead of Cloud Datastore. It worked like a charm!

/**
   * This method lists all the entities inserted in datastore.
   * It uses HTTP GET method and paging support.
   *
   * @return A CollectionResponse class containing the list of all entities
   * persisted and a cursor to the next page.
   */
  @SuppressWarnings({"unchecked", "unused"})
  @ApiMethod(name = "listNote")
  public CollectionResponse<Note> listNote(
    @Nullable @Named("cursor") String cursorString,
    @Nullable @Named("limit") Integer limit) {

    EntityManager mgr = null;
    Cursor cursor = null;
    List<Note> execute = null;

    /* Code generated by GPE, connecting to the cloud datastore, remarked
     * try{
      mgr = getEntityManager();
      Query query = mgr.createQuery("select from Note as Note");
      if (cursorString != null && cursorString != "") {
        cursor = Cursor.fromWebSafeString(cursorString);
        query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
      }

      if (limit != null) {
        query.setFirstResult(0);
        query.setMaxResults(limit);
      }*/
   
      Note notesql = new Note();
   
      execute  = new ArrayList<Note>();

      /* Code retrieving record from Cloud SQL */
      try {
   
     String  = "jdbc:google:mysql://projectid:sqlinstancename/guestbook?user=root";
   
     //Database connection
     Connection conn = DriverManager.getConnection(url);
   
     //SQL query
     String statement = "select * from entries";        
       
     //Result of SQL query
     ResultSet rs = conn.createStatement().executeQuery(
           statement);
          int i = 0;        
       
          //Loop over the query result
          while (rs.next()) {
       
         //Constructing notesql by Cloud database content
         notesql = new Note();
         notesql.setId(String.valueOf(i));
         notesql.setDescription(rs.getString("content"));
         notesql.setEmailAddress(rs.getString("guestName"));
         //Add notesql to a list for return
         execute.add(notesql);
         i++;
          }
   
      } catch (SQLException e) {
     e.printStackTrace();
   }
   
      //Code generated by GPE for storing query result from cloud data store, remarked
      //execute = (List<Note>) query.getResultList();
      cursor = JPACursorHelper.getCursor(execute);
      if (cursor != null) cursorString = cursor.toWebSafeString();

      // Tight loop for fetching all entities from datastore and accomodate
      // for lazy fetch.
      for (Note obj : execute);
   
      //Code generated by GPE for closing cloud data store connection, remarked
   /* } finally {
      mgr.close();
    }*/

    return CollectionResponse.<Note>builder()
      .setItems(execute)
      .setNextPageToken(cursorString)
      .build();
  }

No comments:

Post a Comment

Applying SMA10/20, SMA20/50 as trading signals

This is the comparison for results before and after applying SMA10/20 and SMA20/50 in the stock trader. Background Trading 3 stock ma...