简体   繁体   中英

Trouble with prepared statement

Basically i am having trouble with the method below - say image 2 is null - then the sql statement that gets used should be update ProfileImages set optionalImageTwo = ? where userid = ? "; update ProfileImages set optionalImageTwo = ? where userid = ? "; update ProfileImages set optionalImageTwo = ? where userid = ? "; - However, when i inspect the prepared statement i can see the following

com.mysql.jdbc.PreparedStatement@11568fb5: update ProfileImages set optionalImageTwo = 'i·?' where userid = 'test'

I am calling the method with test, abc

Any ideas why this is not working correctly, as when i run the method again, image 2 is still null.

Thanks

public static boolean addOptionalImages(String userid, String image){
    Connection conn = null;
    PreparedStatement st = null;
    ResultSet rs = null;
    PreparedStatement prepst = null;
    final String getOptionalImages = "SELECT * FROM ProfileImages WHERE userid = '" + userid + "'";

    try {
        conn = source.getConnection();
        st = conn.prepareStatement(getOptionalImages);

        //firstly grab all optional images for the user so we can see
        //if the user has already uploaded this image or not
        String sql = null;
        Blob imageOne;
        Blob imageTwo;
        Blob imageThree;
        Blob imageFour;

        st = conn.prepareStatement(getOptionalImages);
        rs = st.executeQuery(getOptionalImages);

        if(rs.next()){
            imageOne = rs.getBlob(1);
            imageTwo = rs.getBlob(2);
            imageThree = rs.getBlob(3);
            imageFour= rs.getBlob(4);

            //check which image to update in the db
            if(imageOne == null){
                sql = "update ProfileImages set optionalImageOne = ? where userid = ? ";
            }else if(imageTwo == null){
                sql = "update ProfileImages set optionalImageTwo = ? where userid = ? ";
            }else if(imageThree == null){
                sql = "update ProfileImages set optionalImageThree = ? where userid = ? ";
            }else if(imageFour == null){
                sql = "update ProfileImages set optionalImageFour = ? where userid = ? ";
            }
        }
        prepst = conn.prepareStatement(sql);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] imageArray = decoder.decodeBuffer(image);
        Blob blobValue = new SerialBlob(imageArray);
        prepst.setBlob(1,blobValue);
        prepst.setString(2,userid);
        prepst.executeUpdate();
        return true;

    }catch(Exception e){
        e.printStackTrace();
        logger.error("Unable to add additional images for user " + userid, e);
    }finally{
        closeConnection(conn);
        closeResultSet(rs);
        closePreparedStatement(st);
        closePreparedStatement(prepst);
    }
    return false;
}

Aren't you only updating one picture each time you run this? you have else if statement covering all your sql statements. So when the first picture is gotten then the second third and forth picture return null, but your program never gets past the second. there is nothing making it update all 4 pictures for one call other this method. you would have to call it 4 times to get every picture, presuming your sql has all the pictures. PS maybe change this to a while instead of an if else tree.

EDIT :CANT COMMENT YET: Yes, but it only does 1 picture. If they ALL are null, then it only updates the first picture. It would explain why a second picture is always null for you.

my indexes were out

position one was the primary key which wasn't a image/blob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM