Java Import Excel To Mysql Table

I would like to automate the  process by adding more than one excel file... in D folder...
so that it should automatically process more number of excels every 5 minutes....
can this be achieve using thread, if show let me know a piece of code...

Import Excel to Mysql Table is  using Apache POI ,

file.xls Example 
Id	Name	Address
1	Name 1	Address 1
2	Name 2	Address 2

This is code example

import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class ImportData {

     * @param args
    public static void main(String[] args) {
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
            PreparedStatement pstm = null ;
            FileInputStream input = new FileInputStream("D://file.xls");
            POIFSFileSystem fs = new POIFSFileSystem( input );
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            Row row;
            for(int i=1; i<=sheet.getLastRowNum(); i++){
                row = sheet.getRow(i);
                int id = (int) row.getCell(0).getNumericCellValue();
                String name = row.getCell(1).getStringCellValue();
                String address = row.getCell(2).getStringCellValue();
                String sql = "INSERT INTO tablename VALUES('"+id+"','"+name+"','"+address+"')";
                pstm = (PreparedStatement) con.prepareStatement(sql);
                System.out.println("Import rows "+i);
            System.out.println("Success import excel to mysql table");
        }catch(ClassNotFoundException e){
        }catch(SQLException ex){
        }catch(IOException ioe){



Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

replace the line
FileInputStream input = new FileInputStream("D://file.xls");

with a loop that loops through the directory of files e.g.

File dir = new File("/Users/myuserdirectory");
		File[] fileList = null;
			fileList = dir.listFiles();
                         for(File file : fileList){
				//put the rest of your code in this loop


Open in new window

You can then use java.util.Timer to create a timer that runs every n minutes.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.