通过使用servlet我试图在Mysql中插入值,但它存储空值

时间:2015-05-20 07:08:42

标签: java mysql servlets

import com.oreilly.servlet.MultipartRequest;    
import java.io.File;    
import java.io.FileInputStream;    
import java.io.FileOutputStream;    
import java.io.IOException;    
import java.io.PrintWriter;    
import java.sql.DriverManager;    
import java.util.ArrayList;    
import java.util.Enumeration;    
import javax.servlet.ServletContext;    
import javax.servlet.ServletException;    
import javax.servlet.http.HttpServlet;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;

public class upload extends HttpServlet {

   File file2 = null;    
   long size=0;    
   String filePath="";    
   String paramname=null;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");    
        PrintWriter out=response.getWriter();    
        String s1=request.getParameter("fname");
        String s2=request.getParameter("lname");
        String s3=request.getParameter("mobile");
        String s4=request.getParameter("email");
        String s5=request.getParameter("date");
        String s6=request.getParameter("qual");
        String s7=request.getParameter("address");
        String s8=request.getParameter("year");
        String s9=request.getParameter("past");
        String s10=request.getParameter("max");
        String s11=request.getParameter("work");
        String s12=request.getParameter("instal");
        String s13=request.getParameter("adhar");
        String s14=request.getParameter("pan");

    ArrayList list = new ArrayList();
    ServletContext context = getServletContext();
    String dirName =context.getRealPath("\\");
    MultipartRequest multi = new MultipartRequest(request, dirName, 10 * 1024 * 1024);
    int f = 0;
    Enumeration files = multi.getFileNames();   
    while (files.hasMoreElements()) 
    {
        paramname = (String) files.nextElement();
        if(paramname.equals("d1"))
        {         
            paramname = null;
        }
                if(paramname != null && paramname.equals("file"))
        {

            f = 1;
                        filePath = multi.getFilesystemName(paramname);
                      if(filePath == multi.getFilesystemName(paramname)){
                           System.out.println(">>>>>>>>>>>>>>"+filePath);

                        //filePath= userid+".jpg";
                       }
            String fPath = dirName+filePath;                            
                        file2 = new File(fPath);
                        size=file2.length();
                       System.out.println(fPath);                         
            FileInputStream fs = new FileInputStream(file2);
            list.add(fs);

         FileOutputStream fout=new FileOutputStream(new File("F:/artizen/images"              + filePath));
                         int j=0;
                         while((j=fs.read())!=-1){
                         fout.write((byte)j);
                         }
                         fs.close();
                         fout.close();

                }           
try
        {
            Class.forName("com.mysql.jdbc.Driver");
            java.sql.Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/artizen","root","");
            java.sql.PreparedStatement st=cn.prepareStatement("insert into painters(fname,lname,mobile,email,date,qual,address,year,past,max,work,instal,adhar,pan,photo) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            st.setString(1,s1);
            st.setString(2,s2);
            st.setString(3,s3);
            st.setString(4,s4);
            st.setString(5,s5);
            st.setString(6,s6);
            st.setString(7,s7);
            st.setString(8,s8);
            st.setString(9,s9);
            st.setString(10,s10);
            st.setString(11,s11);
            st.setString(12,s12);
            st.setString(13,s13);
            st.setString(14,s14);
            st.setString(15,filePath);

           int i=st.executeUpdate();
            if(i==1)
            {                    
                out.println("VALUES ARE INSERTED SUCCESSFULLY");
               out.println("<align=\"center\"><a href=\"inventory.jsp\">GO BACK TO MAIN PAGE</a>"); 
            }

            }catch(Exception e)
            {
                out.println(e);
            }                        
        }            
    }
}

0 个答案:

没有答案