数组数据未插入mysql数据库

时间:2015-05-23 12:48:19

标签: jsp servlets

我尝试上传excel文件为.csv extention并成功上传文件 我得到了数组中的excel表数据 但我不能传递到mysql数据库连接建立但数据没有传递.i在运行时没有得到任何错误,但即使单个数据没有通过 请帮帮我!

<%@ page import="java.io.*" %> 
<%@ page import="java.sql.*" %> 
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
 <% 
String contentType = request.getContentType(); 
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) 
{ 
DataInputStream in = new DataInputStream(request.getInputStream()); 
int formDataLength = request.getContentLength(); 
byte dataBytes[] = new byte[formDataLength]; 
int byteRead = 0; 
int totalBytesRead = 0; 

while (totalBytesRead < formDataLength) 
{ 
byteRead = in.read(dataBytes, totalBytesRead, formDataLength); 
totalBytesRead += byteRead; 
} 
String file = new String(dataBytes); 
String saveFile = file.substring(file.indexOf("filename=\"") + 10); 
System.out.println("saveFile=" + saveFile); 
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\"")); 
System.out.println("saveFile: " + saveFile); 

saveFile = file.substring(file.indexOf("filename=\"") + 10); 
saveFile = saveFile.substring(0, saveFile.indexOf("\n")); 
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\"")); 

int lastIndex = contentType.lastIndexOf("="); 
String boundary = contentType.substring(lastIndex + 1,contentType.length()); 

int pos; 
pos = file.indexOf("filename=\""); 
pos = file.indexOf("\n", pos) + 1; 
/* pos = file.indexOf("\n", pos) + 1; 
pos = file.indexOf("\n", pos) + 1;  */

int boundaryLocation = file.indexOf(boundary, pos) - 4; 
int startPos = ((file.substring(0, pos)).getBytes()).length; 
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; 
FileOutputStream fileOut = new FileOutputStream(saveFile); 
fileOut.write(dataBytes, startPos, (endPos - startPos)); 
%> 
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database.</b> 
<%
Connection con=null; 
Statement pst=null; 
String line = null; 
String value=null; 
ResultSet rs=null;
try 
{ 
	
	StringBuilder contents = new StringBuilder(); 
    BufferedReader input = new BufferedReader(new FileReader(saveFile)); 
    String id="",name="",age="",address="",pincode="",status="";
   while (( line = input.readLine()) != null) 
 { 
	String arr[]=  line.split(",");
	 for(int i=0 ;i<arr.length;i++)
	 {
		 if(i==0)
		 {
		  id=arr[i];
		  name="";
		  age="";
		  address="";
		  pincode="";
		  status="";
		 }
		 if(i==1)
		 {
			 name=arr[i];
			 age="";
			 address="";
			 pincode="";
			 status="";
		 }
		 if(i==2)
		 {
			 age=arr[i];
			 address="";
			 pincode="";
			 status="";
		 }
		 if(i==3)
		 {
			 address=arr[i];
			 pincode="";
			 status="";
		 }
		 if(i==4)
		 {
			 pincode=arr[i];
			 status="";
		 }
		 if(i==5)
		 {
			 status=arr[i];
		 }
		 
		 
		if(id!=""&&name!=""&&age!=""&&address!=""&&pincode!=""&&status!="")
		{
		 System.out.println("ID: "+id+" Name:"+name+" Age:"+age+" Address:"+address+" pincode:"+pincode+" status:"+status);
		  
		}
		 
		/*  Class.forName("com.mysql.jdbc.Driver"); 
	       Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/movie", "root", "mysql"); 


	     int appid=Integer.parseInt(id);
	     System.out.println("appid:"+appid);
	     
	     System.out.println("name:"+name);
	     
	     int cage=Integer.parseInt(age);
	     System.out.println("age:"+age);
	     
	     System.out.println("address:"+address);
	     
	     int pin=Integer.parseInt(pincode);
	     System.out.println("pin:"+pin);
	     
	     int stat=Integer.parseInt(status);
	     System.out.println("status:"+stat);
	     
	     String progress="open";
	     System.out.println("progress:"+progress);
	     
	     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	      String CurruntDate = sdf.format(new Date());
	     	System.out.println("Currunt date:" +CurruntDate);
	     	
	     	 HttpSession session1=request.getSession(false); 
	     	 String userid=(String)session1.getAttribute("userid");
	     	 System.out.println("session userid:"+userid);
	     	 
	     	 PreparedStatement ps1=(PreparedStatement) con.prepareStatement("insert into transaction values("+appid+",'"+name+"',"+cage+",'"+address+"',"+pin+","+stat+",'"+CurruntDate+"','"+progress+"','"+userid+"')"); 
	          rs=(ResultSet) ps1.executeQuery();
	          // pst=con.createStatement(); 
	        // pst.executeUpdate("insert into transaction values("+appid+",'"+name+"',"+cage+",'"+address+"',"+pin+","+stat+",'"+CurruntDate+"','"+progress+"','"+userid+"')"); 
             */
	 
	 }
     contents.append(line); 
     
 } 

               
         int appid=Integer.parseInt(id);
         System.out.println("appid:"+appid);
         
         System.out.println("name:"+name);
         
         int cage=Integer.parseInt(age);
         System.out.println("age:"+age);
         
         System.out.println("address:"+address);
         
         int pin=Integer.parseInt(pincode);
         System.out.println("pin:"+pin);
         
         int stat=Integer.parseInt(status);
         System.out.println("status:"+stat);
         
         String progress="open";
         System.out.println("progress:"+progress);
         
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         String CurruntDate = sdf.format(new Date());
	     System.out.println("Currunt date:" +CurruntDate);
	     
	     HttpSession session1=request.getSession(false); 
	     String userid=(String)session1.getAttribute("userid");
	     System.out.println("session userid:"+userid);
	 
	     
	     
      Class.forName("com.mysql.jdbc.Driver");
      String url="jdbc:mysql://localhost:3306/movie";
      String username="root";
      String password="mysql";
      String query="insert into transaction(applicationid, customername, customerage, customeraddress, pincode, status, edate, progress, euserid) VALUES("+id+",'"+name+"',"+age+",'"+address+"',"+pincode+","+status+",'"+CurruntDate+"','"+progress+"','"+userid+"') ";
      Connection conn=DriverManager.getConnection(url,username,password);
      System.out.println("connection established");
        //Statement stmt=conn.createStatement();
     // stmt.executeQuery(query); 
	 // System.out.println("ID: "+id+" Name:"+name+" Age:"+age+" Address:"+address+" pincode:"+pincode+" status:"+status);
 	      		  
	     
	   PreparedStatement ps1=(PreparedStatement) con.prepareStatement("insert into transaction(applicationid, customername, customerage, customeraddress, pincode, status, edate, progress, euserid) VALUES("+id+",'"+name+"',"+age+",'"+address+"',"+pincode+","+status+",'"+CurruntDate+"','"+progress+"','"+userid+"') "); 
       ps1.executeUpdate();
       //  pst=con.createStatement();
       // pst.executeUpdate("insert into userone values(111,'vikas','agartala')");
      //pst.executeQuery("insert into transaction(applicationid, customername, customerage, customeraddress, pincode, status, edate, progress, euserid) VALUES("+id+",'"+name+"',"+age+",'"+address+"',"+pincode+","+status+",'"+CurruntDate+"','"+progress+"','"+userid+"') ");

      pst.close();
} 

catch(Exception e) 
{} 

} 
%> 

1 个答案:

答案 0 :(得分:0)

以这种方式编写查询

&#13;
&#13;
String query = "insert into transaction(applicationid, customername, customerage, customeraddress, pincode, status, edate, progress, euserid)
VALUES(?,?,?,?,?,?,?,?,?)";  // The total ? is 9 as your total columns is 9

PreparedStatement ps1=(PreparedStatement) con.prepareStatement(query);
                ps1.setInterger(1,id);   // set your datatype whatever it is 
                ps1.setString(2,name);   // set your datatype whatever it is 
                ps1.setInteger(3,age);   // set your datatype whatever it is 
                ps1.setString(4,address);// set your datatype whatever it is 
                ps1.setInteger(5,pincode);// set your datatype whatever it is 
                ps1.setString(6,status); // set your datatype whatever it is 
                ps1.setString(7,CurrentDate);// set your datatype whatever it is 
                ps1.setString(8,progress);// set your datatype whatever it is 
                ps1.setInteger(9,userid);// set your datatype whatever it is 
                ps1.executeUpdate();
           
&#13;
&#13;
&#13;