File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes how to upload xls file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how to upload xls file" Watch "how to upload xls file" New topic

how to upload xls file

saravanan rajendran
Ranch Hand

Joined: Jun 13, 2008
Posts: 75
hi, i trying to upload a xls.file and import to mysql database please help me

xls.file contain name and id value that i want to store in database

my html page contain browser button when i pressed that i can retrieve that xls file i have import button when i pressed that my xls file value should store in database

my problem:

i was retrieved my xls file in browser how i can get that xls file into that below code please help me

when i pressed import button it will go to below code

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
conn = DriverManager.getConnection("jdbc dbc river={Microsoft Excel Driver (*.xls)};DBQ=" + filename);//how i can get xls file into this code?
st = conn.createStatement();
String query = "Select * from [Sheet1$]" ;
rs = st.executeQuery( query );
while( )
String name=rs.getString(1);
String loc=rs.getString(2);

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/vbtest", "meetme", "meetme");
st = conn.createStatement();
st.executeUpdate("Insert INTO sample1(`name` , `location`)VALUES('"+ name +"' ,'"+ loc +"')");
rs1=st.executeQuery("select * from sample1");
catch(Exception e)
catch( Exception e )
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

Check out java.sql.PreparedStatement, combined with a BLOB field in your database. Open an InputStream to the uploaded file, and call setBinaryStream with it.

There might be a problem with this if you don't know the file size and either your database driver is not built for Java 6, or you're stuck to Java 5.0 or below for some reason - you need to pass the InputStream length (as an int) to the only setBinaryStream method available in Java 5.0 and below.

A solution could be using ByteArrayInputStream and ByteArrayOutputStream:

This could be memory consuming (you're storing the entire XLS file in memory) so it's really preferred if you can just retrieve the XLS file size without reading the contents.

How To Ask Questions How To Answer Questions
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 445


I suspect he wanted to actually get the data in the Excel worksheet into rows in the database (I am assuming this from the query written). For that you should check up on using some third party framework e.g. Apache POI HSSF or ActiveXLS You can then read the data from the Excel sheets and then run them against the database using the query as necessary.

I agree. Here's the link:
subject: how to upload xls file
It's not a secret anymore!