如何使用pojo从数据存储中检索实体?

时间:2016-04-06 11:01:39

标签: java google-app-engine google-cloud-datastore

   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;
}
}

当我尝试执行此操作时,Register是我的pojo类,我得到由NullPointerException引起的INTERNAL_SERVER_ERROR并且我不需要代码来从实体类中检索所有细节 提前谢谢

2 个答案:

答案 0 :(得分:1)

如果你想获得一个对象,你应该使用pm.getObjectById(Register.class,id),如果你想查询如下:

from sys import argv

from os.path import exists

script,from_file,to_file=argv

in_file=open(from_file) in_data=in_file.readlines() count=len(in_data)

#print in_data[3] print count line=in_data[count-1]

in_file.close()

out_file=open(to_file,'w')
out_file.write(line)

out_file.close()
in_file.close()

答案 1 :(得分:1)

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/>");
}

这是从表中获取所有实体btw感谢您的贡献