<repo_name>sravi4701/plack<file_sep>/src/controller/Testsql.java
package controller;



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import getData.GetConnection;
/**
 * Servlet implementation class Testsql
 */
@WebServlet("/Testsql")
public class Testsql extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Testsql() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		
		Connection cn = GetConnection.getCn();
		String query = "select * from test";
		try {
			Statement st = cn.createStatement();
			ResultSet rs = st.executeQuery(query);
			while(rs.next()){
			out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
			}
			cn.close();
			} catch (Exception e) {
			System.out.println(e);
		}
}


}<file_sep>/src/controller/UploadPhoto.java
package controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import getData.GetConnection;

/**
 * Servlet implementation class UploadPhoto
 */
@WebServlet("/UploadPhoto")
public class UploadPhoto extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadPhoto() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String currentUser = request.getParameter("id");
		
		Connection cn = GetConnection.getCn();
		boolean flag = false;
		try{
			String sql = "select image from dashboard where useremail=?";
			PreparedStatement ps = cn.prepareStatement(sql);
			ps.setString(1, currentUser);
			ResultSet rs = ps.executeQuery();
			Blob image = null;
			byte [] imgData = null;
			if(rs.next()){
				image = rs.getBlob(1);
				if(image != null){
					imgData = image.getBytes(1, (int)image.length());
	//				InputStream input = rs.getBinaryStream("photo");
					OutputStream output = response.getOutputStream();
					response.setContentType("image/gif");
					output.write(imgData);	
				}
				else{
					flag=true;
				}
			}
			else{
				flag=true;
			}
			if(flag){
				response.setContentType("image/gif");  
			    ServletOutputStream out;  
			    out = response.getOutputStream();  
			    FileInputStream fin = new FileInputStream("/home/ravishankar/workspace/plack/WebContent/img/default.png");
			    BufferedInputStream bin = new BufferedInputStream(fin);  
			    BufferedOutputStream bout = new BufferedOutputStream(out);  
			    int ch =0;
			    while((ch=bin.read())!=-1)  
			    {  
			    bout.write(ch);  
			    }  
			      
			    bin.close();  
			    fin.close();  
			    bout.close();  
			    out.close();
				System.out.println("UploadPhoto.java: no record found");
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
<file_sep>/src/getData/GetConnection.java
package getData;

import java.sql.*;  
public class GetConnection{
		static String driver = "com.mysql.jdbc.Driver";
		static String url = "jdbc:mysql://localhost:3306/plack";
		static String user = "root";
		static String pass = "<PASSWORD>";
		static Connection cn = null;
		
		public static Connection getCn(){
			try{
				Class.forName(driver);
				cn = DriverManager.getConnection(url, user, pass);
			}
			catch(Exception e){
				System.out.println(e);
			}
			return cn;
		}
	}<file_sep>/README.md
# plack
### A simple web presence of a person
Built with Java web application tools like Servlet, JSP etc.
<file_sep>/src/controller/FileUpload.java
package controller;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import getData.GetConnection;

/**
 * Servlet implementation class FileUpload
 */
@WebServlet("/FileUpload")
@MultipartConfig(maxFileSize = 16177215)
public class FileUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FileUpload() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession s = request.getSession(false);
		String currentUser = (String)s.getAttribute("email");
		InputStream inputStream = null;
		Part filePart = request.getPart("photo");
		System.out.println("FileUpload.java " + currentUser);
		if(filePart != null){
			System.out.println(filePart.getName());
			System.out.println(filePart.getSize());
			System.out.println(filePart.getContentType());
			inputStream = filePart.getInputStream();
		}
		Connection cn = GetConnection.getCn();
		try{
			String sql = "update dashboard set image=? where useremail=?";
			PreparedStatement ps = cn.prepareStatement(sql);
			if(inputStream != null){
				ps.setBlob(1, inputStream);
			}
			ps.setString(2, currentUser);
			int c = ps.executeUpdate();
			if(c > 0){
				System.out.println("sucess");
				response.sendRedirect("Profile.jsp");
			}
			else{
				System.out.println("FileUpload.java : " + "no record found");
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
