简体   繁体   中英

How to retrieve entities from datastore by using pojo?

   package com.myproj;

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

    import javax.jdo.PersistenceManager;
   import javax.jdo.Query;
   import javax.servlet.ServletException;
   import javax.servlet.http.HttpServlet;
   import javax.servlet.http.HttpServletRequest;
   import javax.servlet.http.HttpServletResponse;


   public class Retrieve extends HttpServlet{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request,HttpServletResponse response)                  throws ServletException, IOException
    {
        response.setContentType("text/html");

        PrintWriter out = response.getWriter();
        PersistenceManager pm= PMF.get().getPersistenceManager();
        Query q = pm.newQuery("SELECT * FROM REGISTER"); 

        @SuppressWarnings("unchecked")
        List<Register> ls = (List<Register>) q.execute();
        for (Register result : ls) 
        {   
            String firstName = (String) result.getName();  
            String email = (String) result.getEmail();

            out.println(firstName + " " + email); 
          }
       }
   }


package com.myproj;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

@PersistenceCapable
public class Register {
@Persistent
protected String Name;
public String getName() {
return Name;
}

public void setName(String Name) {
this.Name = Name;
}

@Persistent
protected String UName;
public String getUName() {
return UName;
}

public void setLName(String UName) {
this.UName = UName;
}

@Persistent
protected String email;
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Persistent
protected String pass;
public String getPass() {
return pass;
}

public void setPass(String pass) {
this.pass = pass;
}
}

where Register is my pojo class when i try to execute this i get the INTERNAL_SERVER_ERROR caused by the NullPointerException and i dono the code to retrieve all the details from the entity kind thanks in advance

You should use pm.getObjectById(Register.class, id) if you want to get one object and if you want to query like:

Query query = pm.newQuery(Register.class);
query.setFilter("email== eemail");
query.declareParameters("string eemail");
return (Member)query.execute(email);
PersistenceManager pm= PMF.get().getPersistenceManager();
Query q = pm.newQuery(Register.class); 
@SuppressWarnings("unchecked")
List<Register> ls = (List<Register>) q.execute();
if(!ls.isEmpty())
for (Register result : ls) 
{   
    String firstName = (String) result.getName();  
    String email = (String) result.getEmail();
    String Uname = (String) result.getUName();
    String pass = (String) result.getPass();
    out.println(firstName + "   "+ Uname +" "+ email + " "+pass); 
    out.println("<br/>");
}

this is getting all the entities from the table btw thanks for your contribution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM