Dear Friends,

I have created a file upload utility which will read the .csv files.

My database structure is--
1)id
2)mobile_no
3)address

The administrator will do bulk upload for the above.

If the id and mobile_no are already present in the database...then it will update the existing data.

If doesnot exist than it will make a entry in the database.

Mine everything is working fine, except one thing.

When existing customers I am updating and if there is no value in address field for that particular id and mobile_no than its showing up the error java.lang.ArrayIndexOutOfBoundsException: 2

The problem arises only if the address field is null in the .csv files.

Plz help me out.

I am attaching my code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>
	<head>
		<title></title>
		
		
		<script type="text/javascript">
		function validate(form)
		{
			if (form.file1.value == "")
			{
				alert("You must enter File1.");
				form.file1.focus();
				return (false);
			}
			return (true);
		}
		</script>

	</head> 
	
	<body>
		<form action="display.jsp"  method="POST" enctype="multipart/form-data" onSubmit='return validate(this)'>
		<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr height="150"><td>&nbsp;</td></tr>
			<tr>
				<td valign="top" align="center">
					<table border="0">
						<tr><td colspan="2">&nbsp;</td></tr>
						<tr>
							<td><span class="label">Upload First File:</span></td>
							<td><INPUT type="file" name="file1" id="file1" class=""/></td>
						</tr>
						<tr><td colspan="2">&nbsp;</td></tr>
						<tr>
							<td colspan="2" align="center"><INPUT type="Submit" name="submitBTN" value="Upload FIle" class="button"/></td>
						</tr>
						<tr><td colspan="2">&nbsp;</td></tr>
						
					</table>
				</td>
			</tr>
		</table>
		
		</form>
	</body>
</html>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.File"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.DataInputStream"%>
<%@ page import = "java.sql.*"%>


<html>
<title></title>

<body>
<form>

<%
	
	String contentType = request.getContentType();
	
	if( (contentType != null) && (contentType.indexOf("multipart/form-data")>= 0))
	{    
		

		//Below code is used to read an uploaded file an save it on server

		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);
		saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

		saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));

		BufferedReader input = null;

			String path = "";
				
				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;
				
				path = application.getRealPath("/");
				
				FileOutputStream fileOut = new FileOutputStream(path + saveFile);
				fileOut.write(dataBytes, startPos, (endPos - startPos));
				fileOut.flush();
				fileOut.close();

				// now reading file

				File file2 = new File(path + saveFile);
				StringBuffer sb = new StringBuffer();
				input = new BufferedReader(new FileReader(file2));
				String[] data = null;
				String line = null;

				// databse connection

				
				PreparedStatement pstmt = null;
				Connection conn=null;
				ResultSet rs=null;
				ResultSet rs1=null;
				Statement stmt=null;
				Statement stmt1=null;
				PreparedStatement ps;
				
				
				//DBConnection dbconn=new DBConnection();

				
					Class.forName("com.mysql.jdbc.Driver");
				 
				 
					conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","admin");

					while((line = input.readLine()) != null)
    				{
						
						data = line.split(",");
						
						int len=data[0].length();
    					String id=data[0];
    					String mob=data[1];
    					String add=data[2];
    					System.out.println(add);
    					int len1=data[1].length();
    					int len2=data[2].length();
    					System.out.println(len2);
			
    					if((len != 0) && (len1 == 10))
    					{
    						System.out.println(data[0].trim() + " - " + data[1].trim() + " - " + data[2].trim());
    						stmt=conn.createStatement();
    						rs=stmt.executeQuery("SELECT * FROM load_data where id="+"'"+co+"' " + "and" +
    								" mobile_no="+"'"+mob+"'");
    						
    						if(rs.next())
    						{
    							ps = conn.prepareStatement("update load_data set address = ? where id = ? and mobile_no = ? "); 
    				   	 		
    				   	 	
    				   	 		ps.setString(1, add);
    				   	 		ps.setString(2, co);
    					 		ps.setString(3, mob);
    				    		ps.executeUpdate();
    						}
    						else
    						{
    							ps = conn.prepareStatement("INSERT INTO load_data (id,mobile_no,address) VALUES (?,?,?) "); 
    				   	 		
    				   	 		ps.setString(1, co);
    				   	 		ps.setString(2, mob);
    					 		ps.setString(3, add);
    				    		ps.executeUpdate();	
    						}
    					}
    				}
					out.println("<BR>");
					out.println("The File Name : " + saveFile + " uploaded Successfully.");

	}
%>
</form>
</body>
</html>

That error state it all you went over boundaries of array. Why don't you use ArrayList, Vector or any class that implements Collection interface and can dynamically.
You should move all the processing from JSP to servlet. Any computation or database connectivity is bad thing. For document upload have look at The Commons FileUpload package specifically designed for tasks like your document upload

well...i m really unknown of that...

can i make changes in the existing code to solve the issue?

Yes, that was one of the reason for my reply...

can you help me out in my existing code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.