【问题标题】:UPDATE query gives me trouble in UserDAO.javaUPDATE 查询在 UserDAO.java 中给我带来了麻烦
【发布时间】:2013-10-05 18:43:00
【问题描述】:

我正在尝试更新小型应用程序的用户配置文件.. 该程序正在从以前的会话中获取值,但我没有相应地更新。 这是来自 UserDAO 的代码...

 public String updateUser(userBean user)throws Exception{

        System.out.println("Reached update user");

        String result = null;
        PreparedStatement stmtUpdate = null;

        //Create a Database Connection
        Connection con = ConnectionDAO.getJDBCConnection();
        try{
            System.out.println("Reached Try block");

            con.setAutoCommit(false);                   
            StringBuffer sbUpdate = new StringBuffer();

            System.out.println("String buffer created");

            sbUpdate.append("UPDATE user SET ");

            System.out.println(user.getUser()+ " details updating....");
            System.out.println(user.getFname()+ " ....");
            System.out.println(user.getLname()+ " ....");
            System.out.println(user.getMobileno()+ " ....");
            System.out.println(user.getEmail()+ " ....");
            System.out.println(user.getAddress()+ " ....");
            System.out.println(user.getDes()+ " ....");

            sbUpdate.append(" fname = '" + user.getFname() + "', ");
            sbUpdate.append(" lname = '" + user.getLname() + "', ");
            sbUpdate.append(" mobileno = '" + user.getMobileno() + "', ");
            sbUpdate.append(" email = '" + user.getEmail() + "', ");
            sbUpdate.append(" address = '" + user.getAddress() + "', ");
            sbUpdate.append(" des = '" + user.getDes() + "', ");

            sbUpdate.append(" where user='" + user.getUser() + "'" );

            stmtUpdate = con.prepareStatement(sbUpdate.toString());

            System.out.println("prepare statement created");

            int rows = stmtUpdate.executeUpdate();

            System.out.println("int rows has a value");

            if (rows != 1){
                result = FAILURE;
                System.err.println("Execute update error for user "+ user.getUser());

            }   

            result = SUCCESS;
            ConnectionDAO.commitJDBCConnection(con);
        }catch (SQLException ex){
            result = FAILURE;
            ConnectionDAO.rollbackJDBCConnection(con);

        }
        finally{
            ConnectionDAO.closeStatement(stmtUpdate);
            ConnectionDAO.closeJDBCConnection(con);
        }
        return result;  
    }

控制台节目...

INFO: Server startup in 460 ms
Oct 05, 2013 11:52:21 PM com.kbcss.DAO.UserDAO checkUser
INFO: Logging for user: sri
Reached update user
Reached Try block
String buffer created
sri details updating....
sri ....
sai ....
789456130 ....
123@qwer.com ....
123456 ....
qwert ....
prepare statement created

我所知道的是......

它一直显示到“创建准备语句”,但之后会发生什么? 没什么...程序终止了!! :( 任何想法都非常感谢..

我做错了吗?

PS..我是这个世界的新手!!!!

【问题讨论】:

  • 为了理解问题,在catch块中做ex.printStackTrace();

标签: java mysql session dao


【解决方案1】:

问题似乎是

中的语法错误

sbUpdate.append(" des = '" + user.getDes() + "', ");

尝试删除最后的逗号。这也不是使用准备好的语句的正确方法。

【讨论】:

    【解决方案2】:

    不要在查询中的 WHERE 关键字前面放置逗号。它导致查询静默回滚并返回FAILURE

    【讨论】:

    • 没关系,发生在我们所有人身上。顺便说一句,艾曼是正确的;您需要使用参数化查询(网络上有大量示例)。您的查询目前存在 SQL 注入风险。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    相关资源
    最近更新 更多