使用谷歌应用程序引擎在JDO数据库中更新..它不起作用?

时间:2012-04-03 16:18:25

标签: java google-app-engine servlets persistence jdo

我需要使用java中的jdo更新我的数据库中的一些条目,并且我使用谷歌应用引擎 虽然我正确编写代码,但更新不适用于我? 这是我的代码

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

        String msg = req.getParameter("msg");
        String oldname = req.getParameter("nameold");
        String name = req.getParameter("name");
        String o = req.getParameter("salary");
        int salary = Integer.parseInt(o);
        String datestring = req.getParameter("date");
        Date date = null;
        try {
            date = new SimpleDateFormat("dd-MM-yyyy").parse(datestring);// 10-10-2010
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        PersistenceManager pm = PMF.get().getPersistenceManager();
        Query query = pm.newQuery(User.class, "name == nameParam");

        query.declareParameters("String nameParam");

        List<User> list = (List<User>) query.execute(oldname);

        try {
            for (User obj : list) {

                 {


                obj.name = name;
                obj.salary = salary;
                obj.date = date;
                req.setAttribute("msg1", msg);
                pm.makePersistent(obj);

            }
        } finally {

            pm.close();
            resp.getWriter().println(">>>>>>>>>>");
        }

        RequestDispatcher view = req.getRequestDispatcher("view");

        try {

            view.forward(req, resp);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // resp.sendRedirect("view");

    }

2 个答案:

答案 0 :(得分:0)

通过“没有工作”我假设它没有更新您的数据存储区而不是您遇到错误。 如果是这样,如果想通过此API对数据存储区进行更新,则需要开始并提交JDO事务:

    pm.currentTransaction().begin(); // <<<
    for (User obj : list) {
        obj.name = name;
        obj.salary = salary;
        obj.date = date;
        req.setAttribute("msg1", msg);
        pm.makePersistent(obj);
    }
    pm.currentTransaction().commit(); // <<<

答案 1 :(得分:0)

使用JDO更新对象的一种方法是获取对象,然后在返回对象的PersistenceManager仍处于打开状态时对其进行修改。关闭PersistenceManager时会保留更改。例如:

public void updateUserTitle(User user, String newTitle) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        User e = pm.getObjectById(User.class, user.getId());

            e.setTitle(newTitle);
        } finally {
        pm.close();
    }
}