how can upload doc file into database !!

Dear Everyone,

How can i upload a doc file into Mysql database ....

can any one please give ur suggestions...

i will show u my code

upload.jsp

**********

<form name="uploader" action="uploaded.jsp"

enctype="multipart/form-data">

<div align="center">

<table id="table1" border="1" bordercolor="#ff0000"

cellpadding="0" cellspacing="0" width="50%">

<tbody bgcolor="#c8d8f8">

<tr>

<td bgcolor="#ccccff">

<p align="center">

Resume Upload!

</td>

</tr>

<tr>

<td>

<p align="center">You can upload your

resume..

<p align="center">

<table align="center" border="1"

cellpadding="10" cellspacing="10">

<tbody>

<tr>

<td>

<input name="file" type="file">

<input name="uploadButton" value="Upload"

type="submit">

</td>

</tr>

</tbody>

</table>

</td>

</tr>

</tbody>

</table>

</div>

</form>

uploaded.jsp

***********

<BODY>

<%@ page import="java.sql.*" %>

<%@ page import="java.util.*" %>

<%@ page import="java.io.*" %>

<%

String file1=request.getParameter("file");

int len;

String query;

PreparedStatement pstmt;

int i=0;

Class.forName("com.mysql.jdbc.Driver");

DriverManager.getConnection("jdbc:mysql://localhost:3306Connection conn= /employee","root","");

try{

File file =new File(file1);

if (file==null)

{

%>

<center>Nothing in It</center>

<%

}

else

{

FileInputStream fis =new FileInputStream(file);

len = (int)file.length();

query = ("insert into loader(resume) VALUES(?)");

pstmt = conn.prepareStatement(query);

pstmt.setString(1,file.getName());

pstmt.setInt(2, len);

//method to insert a stream of bytes

pstmt.setBinaryStream(3, fis, len);

i=pstmt.executeUpdate();

if(pstmt!=null)

pstmt.close();

if(conn!=null)

conn.close();

}

}catch (Exception e){

e.printStackTrace();

}

if(i>0)

{

out.println("uploaded");

}

else

{

out.println("not uploaded")

}

%>

</BODY>

[4327 byte] By [senthil_yogaa] at [2007-11-27 9:05:39]
# 1
try search in jsp forum "upload file".String file1=request.getParameter("file"); won't work.
j_shadinataa at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
Reply to J_ShadinataThank u very much for ur suggestions....Take care bye
senthil_yogaa at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Reply to J_ShadinataThank u alot for all ur suggestions...i have given in the JSP forum, but there is no one to help... If it is possible ,you can refer and give ur suggestions later,... Reply would be appreciated....Thank u
senthil_yogaa at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
check here: http://www.javazoom.net/jzservlets/uploadbean/uploadbean.html
j_shadinataa at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Reply To J_Shadinata

Thank u for ur suggestions

Yes i was able to see that , but how can i implement it....

i will explain u the application ie i have upload resume of each employee into database...

As u well know about my web application....

please give ur suggestions...please.........

senthil_yogaa at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

You could just use a BLOB type to store the raw bytes of the file in the database. In that case you might want to use a DataInputStream and concatenate all the bytes together. I think that's a bad solution though.

You should save the file to the hard drive. For security purposes, it's best to keep the files in a non-disclosed location on the hard drive that's inaccessible to web users. Then when it comes time to store it in the database, just store the filename in a column. When it comes time to retrieve the file, query the DB for the name, then grab the file at that location.

try this link for more information:

http://www.oop-reserch.com/cross_servlet.html

-C. R.

C.R.Crena at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

File file = new File("myDoc.doc");

FileInputStream fis = new FileInputStream(file);

PreparedStatement ps =

conn.prepareStatement("insert into DocTablevalues (?,?)");

ps.setString(1,file.getName());

ps.setBinaryStream(2,fis,(int)file.length());

ps.executeUpdate();

ps.close();

fis.close();

java_2006a at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
Thank u for ur suggestions....can u please implementthe code in the above, show me...please......
senthil_yogaa at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

> File file = new File("myDoc.doc");

> FileInputStream fis = new FileInputStream(file);

The file name will most likely NOT be hard-coded. I don't think the question surrounding this topic is how to do the insert statement (he/she clearly knows SQL).

HTTP and the web browser take care of the file transfer, but the server stores the file in a temporary location. The question is, "How do I found out that location?" In PHP (I know that doesn't offer much help to you, but ...), that location is held in a variable called $_FILES. From there you can simply copy the file to a location you specify (probably with a user-specified name under a directory specifically for that user).

I hope this is of some help:

http://www.oop-reserch.com/mime_example_4.html

Good Luck,

C. R.

C.R.Crena at 2007-7-12 21:40:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...