Results 1 to 6 of 6
Thread: How to solve Exception?
- 08-26-2010, 05:49 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 28
- Rep Power
- 0
How to solve Exception?
Hello all,
I am inserting file data into mysql . i am getting index out of bound exception .. can any one tell me how to solve this exception?
Here is the code:(.jsp)
<%--
Document : page
Created on : Aug 21, 2010, 11:42:38 AM
Author : gbrashmi
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hello World!</h2>
<FORM ENCTYPE="multipart/form-data" ACTION=
"upload_page.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE WITH CURRENT DATE AND TIME</B><center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><INPUT TYPE="submit" VALUE="Send File" ></td>
</tr>
<table>
</center>
</FORM>
</body>
</html>
////////////////////////////
Database connection file(.jsp):
<%--
Document : upload_page
Created on : Aug 21, 2010, 11:44:09 AM
Author : gbrashmi
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.sql.*,java.util.*,java.text .*,java.text.SimpleDateFormat" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int val =0;
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));
%>
<%
Connection con=null;
PreparedStatement pstatement = null;
String line = null;
String value=null;
String url = "jdbc:mysql://localhost/gb";
String dbName = "gb";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "admin";
try{
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while (( line = input.readLine()) != null){
contents.append(line);
}
value = contents.toString();
System.out.println("Value:"+value);
Class.forName("com.mysql.jdbc.Driver").newInstance ();
con = DriverManager.getConnection(url,userName,password) ;
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now) ;
String queryString = "INSERT INTO file_tbl set file_data='"+value+"',file_date='"+strDateNew+"'";
//out.println(queryString);
pstatement=con.prepareStatement(queryString);
val = pstatement.executeUpdate();
if(val>0)
{
%>
<br><br>
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database at <%=strDateNew%>.</b>
<%
}
}
catch(Exception e)
{}
}
%>
</body>
</html>
Thanks in advance
Rashmi
- 08-26-2010, 09:19 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Shift all that db code out of the JSP and stick it in it's own class that is called from a Servlet.
However, files are usually stored as BLOBs or CLOBs, not as huge strings...and dates are stored as DATEs not as Strings. And use a PreparedStatement. Concatenating SQL together like that is wrong.
- 08-26-2010, 09:53 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 28
- Rep Power
- 0
Thanks for the response.
database connection code is in separate jsp file..
- 08-26-2010, 10:23 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Nope.
You do not put business logic into a JSP page.
They are for display.
So, again, shift that db code out into it's own class (or classes) and call it from a Servlet, which then passes the relevant data to the JSP...for display.
- 08-26-2010, 02:02 PM #5
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
Can you just post the exception ? which will help in identifying the bug.
Arun K R,Bangalore,India
:)
- 08-27-2010, 06:52 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 28
- Rep Power
- 0
Hello arun,
here is the exception:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.StringIndexOutOfBoundsException: String index out of range: -143764
org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:541)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:435)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:390)
root cause
java.lang.StringIndexOutOfBoundsException: String index out of range: -143764
java.lang.String.substring(String.java:1938)
org.apache.jsp.upload_005fpage_jsp._jspService(upl oad_005fpage_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet .java:803)
org.netbeans.modules.web.monitor.server.MonitorFil ter.doFilter(MonitorFilter.java:390)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
Apache Tomcat/6.0.14
Similar Threads
-
how to solve exception in jsp?
By gb.rashu in forum JavaServer Pages (JSP) and JSTLReplies: 9Last Post: 08-06-2010, 05:04 PM -
How to solve this "Fixtures code" error exception?
By makerror in forum New To JavaReplies: 4Last Post: 12-02-2009, 02:45 AM -
Plz solve this....
By theone3nu in forum Java 2DReplies: 3Last Post: 01-08-2009, 05:01 PM -
how to solve transformer exception
By java_arc in forum Advanced JavaReplies: 1Last Post: 04-19-2008, 12:46 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks