UPDATE查询在UserDAO.java中给我带来麻烦

时间:2013-10-05 18:43:00

标签: java mysql session dao

我正在尝试更新小型应用程序的用户配置文件。 该程序正在从之前的会话中获取值,但我没有相应地更新。 这是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 ..我是这个世界的新手!!!!

2 个答案:

答案 0 :(得分:1)

问题似乎是

中的语法错误

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

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

答案 1 :(得分:1)

请勿在查询中的WHERE关键字前面放置逗号。它导致查询以静默方式回滚并返回FAILURE