gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:44:23 26 4
gpt4 key购买 nike

   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,并且我不使用代码从实体类型检索所有详细信息提前致谢

最佳答案

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

Query query = pm.newQuery(Register.class);
query.setFilter("email== eemail");
query.declareParameters("string eemail");
return (Member)query.execute(email);

关于java - 如何使用 pojo 从数据存储中检索实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com