wood burning stoves 2.0*
The moose likes JDBC and the fly likes Need a help to rectify my error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need a help to rectify my error" Watch "Need a help to rectify my error" New topic

Need a help to rectify my error

Deepthi Xavy
Ranch Hand

Joined: Aug 31, 2009
Posts: 32
<%@ page import="java.io.*,java.sql.*" %>
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) + 2;
pos = file.indexOf("\n", pos) + 3;
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;
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while (( line = input.readLine()) != null){
value = contents.toString();
int val = pst.executeUpdate("insert into file(file_data) values('"+value+"')");
catch(Exception e)

Am doing a csv file import & saving in database.two jsp pages are used,one is to browse & the other page is to save in the database.I got the output but the problem is that all the row values are geting added in the same column in database,where do i want to correct so that i get a proper output such that rows has to save in row wise & column has to get saved in column wise..

Can anyone get me out from this point..
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41108
Please edit your post to UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.

Looking briefly at the code, I noticed the following issues. Firstly, you should never do this:
catch (Exception e)

How will you know if there are problems? At least print a message to a log file or console.

String file = new String(dataBytes);

This assumes that the file is encoded in the default encoding of the server JVM - which is quite likely not the case. Use the constructor that allows you to specify the encoding.

I would strongly suggest to use a CSV processing library (you can find several here); CSV has a number of features that aren't implemented by your code, and which might lead to problems later if they happen to occur within the file.

Ping & DNS - my free Android networking tools app
Don't get me started about those stupid light bulbs.
subject: Need a help to rectify my error
Similar Threads
JSP Page For Uploading a File to Destination Folder
request cannot be resolved. any imports missing??
Problem in Uploading Text File
moving files over the network
Uploading file path in properties file