Advertisement
Guest User

Untitled

a guest
Sep 30th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. public void doGet(HttpServletRequest httpRequest,HttpServletResponse httpResponse)throws ServletException,IOException{
  2. Statement statement= null;
  3. ResultSet resultSet;
  4. InputStream inputStream;
  5. String SQL;
  6. try {
  7. String fileId= httpRequest.getParameter("Id");
  8. System.out.println("inside servlet�>" +fileId);
  9. Connection connection= databaseConnect.getConnection();
  10. statement= connection.createStatement();
  11. SQL= "SELECT file FROM Registration WHERE id= '"+fileId+"'";
  12. resultSet= statement.executeQuery(SQL);
  13. if(resultSet.next()){
  14. byte[]byteArray= new byte[524288];
  15. int size= 0;
  16. inputStream= resultSet.getBinaryStream(1);
  17. httpResponse.reset();
  18. httpResponse.setContentType("image/jpeg");
  19. while((size= inputStream.read(byteArray))!= -1){
  20. httpResponse.getOutputStream().write(byteArray, 0, size);
  21. }
  22. }
  23. } catch(IOException eIO) {
  24. System.out.println("I/O error in retreiveImage servlet"+eIO);
  25. }catch(Exception e){
  26. System.out.println("Error in retreiveImage servlet"+e);
  27. }
  28. }
  29.  
  30. <h:outputLink id="file" value="retreiveImage?Id=#{bean.id}" target="_blank">
  31. <h:graphicImage value="retreiveImage?Id=#{bean.id}" width="50" height="75">
  32. <h:inputFile value="#{bean.file}"></h:inputFile>// Попытка вставить новое изображение
  33. </h:graphicImage>
  34.  
  35. public static String updateUserRecord(manager updateRecord){
  36. try {
  37. SQL= "UPDATE Registration SET userName=?, email=?, password=?, file=?, timeStamp=? WHERE id=?";
  38. prepStatement= databaseConnect.getConnection().prepareStatement(SQL);
  39. prepStatement.setString(1, updateRecord.getUserName());
  40. prepStatement.setString(2, updateRecord.getEmail());
  41. prepStatement.setString(3, updateRecord.getPassword());
  42. inputStream= file.getInputStream();
  43. prepStatement.setBinaryStream(4, inputStream, (int)file.getSize());
  44. //create a java timestamp object that represents the current time ("current timestamp")
  45. Calendar calendar= Calendar.getInstance();
  46. Timestamp timeStamp= new Timestamp(calendar.getTime().getTime());
  47. prepStatement.setTimestamp(5, timeStamp);
  48. prepStatement.setInt(6, updateRecord.getId());
  49. prepStatement.executeUpdate();
  50. System.out.println("User updated Successfully!");
  51. FacesMessage message= new FacesMessage("User updated!");
  52. FacesContext.getCurrentInstance().addMessage("editForm: updateBtn", message);
  53. prepStatement.close();
  54. } catch (SQLException sqlEx) {
  55. sqlEx.printStackTrace();
  56. }catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. return "administration";
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement