4 Replies - 514 Views - Last Post: 08 May 2013 - 09:11 AM

#1 babu786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-May 13

question jsp codes image and data in myslq database

Posted 03 May 2013 - 08:53 AM

Can anyone help me..... I want to upload image and data in myslq database and display in another jsp page .I want whole coding if anyone can help.
I used this codes but it does not store the image in database it can save it to a folder

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
String saveFile="";
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);
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;
File ff = new File("C:/Tomcat 7.0/webapps/am/notes/"+saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><table border="0" width="100%" id="table1">
	<tr>
		<td background="untitled.bmp">&nbsp;<p>&nbsp;</p>
		<p>&nbsp;</td>
	</tr>
</table>
<center><table width=100% bgcolor="#FFFFFF" id="table2">
<tr>
<td width=15% align="center" bgcolor="#3366FF"><b><font color="white" size=4>
<a href="index.html"><font color="#FFFFFF">Home</font></a></font></b><td width=5% align="center" bgcolor="#3366FF">
<u><b><font color="white" size=4>
|</font></b></u><td width=15% align="center" bgcolor="#3366FF"><u><b><font color="white" size=4>About Us</font></b></u><td width=5% align="center" bgcolor="#3366FF">
<u><b><font color="white" size=4>
|</font></b></u><td width=15% align="center" bgcolor="#3366FF">
<font color="#FFFFFF" size="4">
<span style="font-family: Times New Roman; font-weight: 700">
<a href="informn.jsp"><font color="#FFFFFF">Latest 
announcements </font></a> </span></font><td width=5% align="center" bgcolor="#3366FF">
<u><b><font color="white" size=4>|
</font></b></u><td width=15% align="center" bgcolor="#3366FF"><b><font color="white" size=4>
<a href="logincheck2.jsp"><font color="#FFFFFF">
Student Zone</font></a></font></b><td width=5% align="center" bgcolor="#3366FF">
<u><b><font color="white" size=4>|
</font></b></u><td width=15% align="center" bgcolor="#3366FF">
<a href="logincheck1.jsp">
<b><font color="#FFFFFF" size=4>Admin Login</font></b></a><u><font color="#FFFFFF">
</font>
</u>
</table>
<p>&nbsp;</p>
</center>
<table border="0" width="100%" id="table3">
	<tr>
		<td>&nbsp;</td>
		<td width="630"><table border="2" id="table4"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>
		</td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td width="630">&nbsp;</td>
	</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><Br></p>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/university";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
File f = new File(saveFile);
psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
fis = new FileInputStream(f);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
int s = psmnt.executeUpdate();
if(s>0){
System.out.println("Uploaded successfully !");
}
else{
System.out.println("Error!");
}
}
catch(Exception e){e.printStackTrace();}
}
%>

This post has been edited by Dormilich: 03 May 2013 - 08:57 AM
Reason for edit:: please use [CODE] [/CODE] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: question jsp codes image and data in myslq database

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3402
  • View blog
  • Posts: 9,619
  • Joined: 08-June 10

Re: question jsp codes image and data in myslq database

Posted 03 May 2013 - 08:59 AM

Moved to Java Forum. Javascript ≠ Java!
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3637
  • View blog
  • Posts: 5,764
  • Joined: 08-June 10

Re: question jsp codes image and data in myslq database

Posted 03 May 2013 - 09:00 AM

View Postbabu786, on 03 May 2013 - 03:53 PM, said:

I want whole coding if anyone can help.

You expect us to write the code for you? That's not what we do here. If you just want somebody to do your work for you, there are plenty of freelance programmers out there who would no doubt be happy to work for you on this. - If, however, you are looking to learn how to do this yourself, then you are in the right place. Just tell us which part of your code you are having trouble with and we'll try to guide you to a solution.

And next time, please post your code examples in [code] tags.

:code:
Was This Post Helpful? 1
  • +
  • -

#4 babu786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-May 13

Re: question jsp codes image and data in myslq database

Posted 03 May 2013 - 09:07 AM

Sorry for that ,I want to save it to mysql database here I have problem it does not save the path to data base :
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/university";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
File f = new File(saveFile);
psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
fis = new FileInputStream(f);

This post has been edited by Atli: 03 May 2013 - 09:09 AM
Reason for edit:: Fixed the [code] ... [/code] tags.

Was This Post Helpful? 0
  • +
  • -

#5 babu786  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-May 13

Re: question jsp codes image and data in myslq database

Posted 08 May 2013 - 09:11 AM

Got the answer no help from any site found it by myself .
Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1