Dear Friends,

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

My database structure is--

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"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

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

		<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>
				<td valign="top" align="center">
					<table border="0">
						<tr><td colspan="2">&nbsp;</td></tr>
							<td><span class="label">Upload First File:</span></td>
							<td><INPUT type="file" name="file1" id="file1" class=""/></td>
						<tr><td colspan="2">&nbsp;</td></tr>
							<td colspan="2" align="center"><INPUT type="Submit" name="submitBTN" value="Upload FIle" class="button"/></td>
						<tr><td colspan="2">&nbsp;</td></tr>
<%@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.*"%>



	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));

				// 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();


					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];
    					int len1=data[1].length();
    					int len2=data[2].length();
    					if((len != 0) && (len1 == 10))
    						System.out.println(data[0].trim() + " - " + data[1].trim() + " - " + data[2].trim());
    						rs=stmt.executeQuery("SELECT * FROM load_data where id="+"'"+co+"' " + "and" +
    								" mobile_no="+"'"+mob+"'");
    							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 = conn.prepareStatement("INSERT INTO load_data (id,mobile_no,address) VALUES (?,?,?) "); 
    				   	 		ps.setString(1, co);
    				   	 		ps.setString(2, mob);
    					 		ps.setString(3, add);
					out.println("The File Name : " + saveFile + " uploaded Successfully.");

7 Years
Discussion Span
Last Post by madristaa

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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.