Guest User

Getimage

a guest
Sep 17th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import javax.servlet.RequestDispatcher;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15.  
  16. import com.mysql.jdbc.Connection;
  17. import com.mysql.jdbc.PreparedStatement;
  18.  
  19. /**
  20.  * Servlet implementation class Getimage
  21.  */
  22. @WebServlet("/Getimage")
  23. public class Getimage extends HttpServlet {
  24.     private static final long serialVersionUID = 1L;
  25.        
  26.     /**
  27.      * @see HttpServlet#HttpServlet()
  28.      */
  29.     public Getimage() {
  30.         super();
  31.         // TODO Auto-generated constructor stub
  32.     }
  33.  
  34.     /**
  35.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  36.      */
  37.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  38.         // TODO Auto-generated method stub
  39.     }
  40.  
  41.     /**
  42.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  43.      */
  44.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  45.         Infoutente foto=new Infoutente();
  46.         // mi serve per generare l'errore
  47.         Newuser nw = new Newuser();
  48.         HttpSession session = request.getSession();
  49.         String connectionURL = "jdbc:mysql://localhost:3306/imagesharing";
  50.         Connection connection = null;
  51.         PreparedStatement statement=null;
  52.         ResultSet rs=null;
  53.         String query;
  54.         int codfoto=0;
  55.         String url=null;
  56.         String datacaricamento=null;
  57.         String didascalia=null;
  58.         Integer codealbum=0;
  59.        
  60.             try {
  61.                 Class.forName("com.mysql.jdbc.Driver").newInstance();
  62.             } catch (InstantiationException e) {
  63.                 // TODO Auto-generated catch block
  64.                 e.printStackTrace();
  65.             } catch (IllegalAccessException e) {
  66.                 // TODO Auto-generated catch block
  67.                 e.printStackTrace();
  68.             } catch (ClassNotFoundException e) {
  69.                 // TODO Auto-generated catch block
  70.                 e.printStackTrace();
  71.             }
  72.            
  73.            
  74.             try {
  75.                 connection = (Connection) DriverManager.getConnection(connectionURL , "root", "0000");
  76.                     codealbum=Integer.parseInt(request.getParameter("album"));
  77.                     foto.setCodupload(codealbum);
  78.                     query="select * from foto,album where album.codalbum=foto.codalbum and album.codalbum=?";
  79.                     statement=(PreparedStatement) connection.prepareStatement(query);
  80.                     statement.setInt(1,codealbum);
  81.                     rs = statement.executeQuery();
  82.                
  83.            
  84.                 while(rs.next()){
  85.                     foto.setCodalbum(rs.getInt("codalbum"));
  86.                     foto.setCodfoto(rs.getInt("codfoto"));
  87.                     foto.setDatacaricamento(rs.getString("datacaricamento"));
  88.                     foto.setDidascalia(rs.getString("didascalia"));
  89.                     foto.setUrl(rs.getString("url"));
  90.                 }
  91.             } catch (SQLException e) {
  92.                 // TODO Auto-generated catch block
  93.                 e.printStackTrace();
  94.             }
  95.            
  96.             request.setAttribute("foto",foto);
  97.             RequestDispatcher disp=request.getRequestDispatcher("slider.jsp");
  98.             disp.forward(request, response);
  99.            
  100.             try {
  101.                 rs.close();
  102.                 statement.close();
  103.                 connection.close();
  104.             } catch (SQLException e) {
  105.                 // TODO Auto-generated catch block
  106.                 e.printStackTrace();
  107.             }
  108.  
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment