import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class Upload extends HttpServlet
{
  Connection con;
  PreparedStatement ps1;
  File f1;
  FileInputStream fis;
  HttpSession session;
  String uname;
  String upload;
  int l1;
  int x;
  PrintWriter pw;
  String lebel;
  String section;

  public void init(ServletConfig paramServletConfig)
  {
    try
    {
 
         Class.forName("com.mysql.jdbc.Driver").newInstance();        
      this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/link","root","root");
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  public void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
  {
    try {
      this.pw = paramHttpServletResponse.getWriter();
      this.session = paramHttpServletRequest.getSession();
      this.uname = ((String)this.session.getValue("uname"));
      System.out.println("Upload java :" + this.uname);
      this.upload = paramHttpServletRequest.getParameter("upload");
      this.lebel = paramHttpServletRequest.getParameter("level");
      this.section = paramHttpServletRequest.getParameter("section");

      if (!(this.upload.equals("")))
      {
        this.f1 = new File(this.upload);
        this.fis = new FileInputStream(this.f1);
        this.l1 = (int)this.f1.length();
        String str1 = this.f1.getName();
        System.out.println(str1);

        String str2 = this.upload.substring(this.upload.lastIndexOf(".") + 1);
        String str3 = str1.substring(0, str1.lastIndexOf("."));
        this.ps1 = this.con.prepareStatement("insert into upload values(?,?,?,?,?,?)");
        this.ps1.setString(1, this.uname);
        this.ps1.setBinaryStream(2, this.fis, this.l1);
        this.ps1.setString(3, str3);
        this.ps1.setString(4, str2);
        this.ps1.setString(5, this.lebel);
        this.ps1.setString(6, this.section);

        this.x = this.ps1.executeUpdate();
        if (this.x > 0)
        {
          this.pw.println("<script language=\"javaScript\">");
          this.pw.println("alert('Uploaded Successfully.!!');");
          this.pw.println("history.back('Upload.jsp')");
          this.pw.println("</script>");
        }
        else
        {
          this.pw.println("<script language=\"javaScript\">");
          this.pw.println("alert('File Uploading Failed!!!');");
          this.pw.println("history.back('Upload.jsp')");
          this.pw.println("</script>");
          this.pw.println("</body>");
        }
        this.fis.close();
      }
      else
      {
        this.pw.println("<script language=\"javaScript\">");
        this.pw.println("alert('Select a File to Upload!!!');");
        this.pw.println("history.back('Upload.jsp')");
        this.pw.println("</script>");
        this.pw.println("</body>");
      }
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }
}