在java中这个更新查询有什么问题?[英] What is wrong with this update query in java?

本文是小编为大家收集整理的关于在java中这个更新查询有什么问题?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试在托管bean中进行更新查询.这是代码:

try {
       PreparedStatement checkDB1 = (PreparedStatement) con.prepareStatement("update pocket set balance=? where username=?");
       checkDB1.setDouble(1, (double) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedUserBalance")-total); //This returns the desired result
       checkDB1.setString(2, (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedAccountIban")); //Returns the desired result

       checkDB1.executeQuery();                

   } catch (SQLException e) {
       System.err.println("SQLState: " + ((SQLException)e).getSQLState());
            System.out.println("SQLState: " +((SQLException)e).getSQLState());
   }

我看到参数已正确设置,看起来我的查询中存在错误.它进入捕获块.另外,我不知道如何看到堆栈跟踪,e.printstacktrace在这里不起作用?可以帮助吗?

谢谢

推荐答案

尝试使用executeupdate而不是executeQuery.我在执行DB更新时看到了executequery的奇怪问题.

本文地址:https://www.itbaoku.cn/post/597313.html

问题描述

I am trying to make an update query in my managed bean. Here is the code:

try {
       PreparedStatement checkDB1 = (PreparedStatement) con.prepareStatement("update pocket set balance=? where username=?");
       checkDB1.setDouble(1, (double) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedUserBalance")-total); //This returns the desired result
       checkDB1.setString(2, (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedAccountIban")); //Returns the desired result

       checkDB1.executeQuery();                

   } catch (SQLException e) {
       System.err.println("SQLState: " + ((SQLException)e).getSQLState());
            System.out.println("SQLState: " +((SQLException)e).getSQLState());
   }

I see that parameters are set correctly, looks like there is an error in my query. It enters the catch block. Also, i don't know how to see the stack trace, e.printStackTrace does not work here how can i see it? Can anyne help?

Thanks

推荐答案

Try using executeUpdate instead of executeQuery. I have seen strange problem with executeQuery while doing a db update.