Pages

Monday, May 31, 2010

Deleting All entries in the Google Datastore locally

The Google Server (App Engine), simulates the development server on the local machine using a file called as local_db.bin in the WEB-INF/appengine-generated/ directory. (It is not uploaded with your application.)


http://code.google.com/intl/fr/appengine/docs/java/tools/devserver.html#Using_the_Datastore

To delete all entries, stop the server, delete this file and then Start the server again.

Friday, May 28, 2010

Searching the Student Objects stored in the Datastore by their Name

Persistents of Student objects are stored in the datastore. One of their properties is their name. The following is the Java Servlet that searches the objects by their name which match the token and returns them to the JSP Page.




import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;




@SuppressWarnings("serial")
public class NameSearch extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {

String token = req.getParameter("search");
PersistenceManager pm=PMF.get().getPersistenceManager();
String query= "select from " + Student.class.getName();
List students= (List) pm.newQuery(query).execute();
ArrayList results = new ArrayList();
for (Student a:students)
{
// Checking whether token appears in the name whether lower or upper
if (a!=null && token!=null)
if(a.getName().toLowerCase().indexOf(token.toLowerCase()) != -1)
{
// Adding the reference to the object into the ArrayList
System.out.println(a.getName());
results.add(a);
}
}
req.setAttribute("result", results);
//Sending the results data via RequestDispatcher to your JSP Page.
                RequestDispatcher r=req.getRequestDispatcher("SearchAS.jsp");
try 
{
r.forward(req, resp);
} catch (ServletException e) 
{
e.printStackTrace();
}



}
}

Thursday, May 27, 2010

Converting String to a date object

When date is input in a form, at the server end it is received as a String, so numerous times it happens that the Date String has to be converted into the Date Object. Here is the code:


01.package org.kodejava.example.java.util;
02. 
03.import java.text.DateFormat;
04.import java.text.SimpleDateFormat;
05.import java.text.ParseException;
06.import java.util.Date;
07. 
08.public class StringToDate
09.{
10.public static void main(String[] args)
11.{
12.DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
13. 
14.try
15.{
16.Date today = df.parse("20/12/2005");           
17.System.out.println("Today = " + df.format(today));
18.} catch (ParseException e)
19.{
20.e.printStackTrace();
21.}
22.}
23.}