基本上我有下面的方法麻煩 - 比如像2爲空 - 那麼被使用的SQL語句應該是update ProfileImages set optionalImageTwo = ? where userid = ? ";
- 但是,當我檢查準備好的聲明中,我可以看到下面的麻煩用事先準備好的聲明
[email protected]:update ProfileImages set optionalImageTwo ='i·?'其中userid =「測試」
我打電話用的測試方法,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;
}
我不明白爲什麼你使用的第一個PreparedStatement對象,因此錯誤,以正確的方式,第二主鍵= \ –
好吧,這很好,它應該只是一個聲明,而不是準備好聲明,但這不會導致錯誤的權利? – Biscuit128
我在這裏沒有看到任何問題,它似乎是一個錯誤,嘗試另一個userId,看看問題是否仍然存在。 –