日期:2014-05-20  浏览次数:20833 次

json字符串转换成对象,如何判断某个字段为空
各位大侠,我后台接受到前台传来的json字符串,然后用工具转换为Java对象,原先的设想如果对象的某个字段为空,那么说明这个对象的这个字段是不需要做修改的,贴上我的代码
Java code
     Connection conn = MySqlTool.getMySqlConnection();
        
        
        PreparedStatement ps = null;
        String sql = "";
        try {
            if(groupObj.getGroupAddress()!=null || groupObj.getGroupAddress().trim().length()!=0){
                
                sql = "update t_group set groupAddress = ? where groupKey = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, groupObj.getGroupAddress());
                ps.setString(2, groupObj.getGroupKey());
                ps.executeUpdate();
            }
            
            if(groupObj.getGroupDescription()!= null || groupObj.getGroupDescription().length() != 0){
                sql = "update t_group set groupDescption = ? where groupKey = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, groupObj.getGroupDescription());
                ps.setString(2, groupObj.getGroupKey());
                ps.executeUpdate();
            }
            
            if(groupObj.getGroupName() != null || groupObj.getGroupName().length() != 0){
                sql = "update t_group set groupName = ? where groupKey = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, groupObj.getGroupName());
                ps.setString(2, groupObj.getGroupKey());
                ps.executeUpdate();
            }
            
            ps.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            
            MySqlTool.closeMySqlConnection(conn);
        }
现在的情况是判断是否为空的if语句成了摆设为空的情况写也执行了里面的sql语句,那就很郁闷了,请问各位大侠我的判断条件是错误的吗?

------解决方案--------------------
恭喜楼主,提供我自己的想法,可以在前台拼json的时候不拼这两个字段不就结了嘛,O(∩_∩)O哈哈~