簡體   English   中英

准備好的語句有問題

[英]Trouble with prepared statement

基本上我在下面的方法上遇到麻煩-假設image 2為null-然后使用的sql語句應該是update ProfileImages set optionalImageTwo = ? where userid = ? "; update ProfileImages set optionalImageTwo = ? where userid = ? "; update ProfileImages set optionalImageTwo = ? where userid = ? "; --但是,當我檢查准備好的語句時,我可以看到以下內容

com.mysql.jdbc.PreparedStatement@11568fb5:更新ProfileImages設置optionalImageTwo ='i·?' 其中userid ='test'

我正在用測試調用該方法,abc

為什么這不能正常工作的任何想法,當我再次運行該方法時,圖像2仍然為空。

謝謝

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;
}

您不是每次運行都只更新一張圖片嗎? 您還有其他if語句覆蓋了您所有的sql語句。 因此,當獲得第一張圖片時,第二張第三張和第四張圖片將返回null,但是您的程序永遠不會超過第二張圖片。 沒有其他方法可以讓它為一次調用更新所有4張圖片。 您必須調用它4次才能獲取每張圖片,前提是您的sql具有所有圖片。 PS可能會將其更改為一會兒,而不是if樹。

編輯:注釋還沒有:是的,但是它只處理1張圖片。 如果它們全部為空,則僅更新第一張圖片。 它可以解釋為什么第二張圖片總是對您無效。

我的索引出來了

位置一是主鍵,不是圖像/斑點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM