excel

excel

how to save excel sheet data into the database using poi api?

View Answers

December 23, 2011 at 12:57 PM

import java.io.*; import java.sql.*; import java.util.*; import java.util.regex.*; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.poifs.filesystem.POIFSFileSystem;

  public class InsertExcelFileDataIntoDatabase {
  public static void main( String [] args ) {
    String fileName="test.xls";
    Vector dataHolder=read(fileName);
    saveToDatabase(dataHolder);
}
    public static Vector read(String fileName)    {
    Vector cellVectorHolder = new Vector();
    try{
            FileInputStream myInput = new FileInputStream(fileName);
            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
       Iterator rowIter = mySheet.rowIterator(); 
       while(rowIter.hasNext()){
              HSSFRow myRow = (HSSFRow) rowIter.next();
              Iterator cellIter = myRow.cellIterator();
              Vector cellStoreVector=new Vector();
              while(cellIter.hasNext()){
                      HSSFCell myCell = (HSSFCell) cellIter.next();
                      cellStoreVector.addElement(myCell);
              }
              cellVectorHolder.addElement(cellStoreVector);
      }
    }catch (Exception e){e.printStackTrace(); }
    return cellVectorHolder;
}
    private static void saveToDatabase(Vector dataHolder) {
    String name="";
            String address="";
            for (int i=0;i<dataHolder.size(); i++){
               Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
                    for (int j=0; j < cellStoreVector.size();j++){
                            HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
                            String st = myCell.toString();
                             name=st.substring(0,1);
                             address=st.substring(0);
                                                    }
                    try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
    Statement stat=con.createStatement();
    int k=stat.executeUpdate("insert into person(Name,Address) value('"+name+"','"+address+"')");
    System.out.println("Data is inserted");
    stat.close();
    con.close();
    }
    catch(Exception e){}
    }
    }
  }

December 23, 2011 at 12:58 PM

import java.io.*;
      import java.sql.*;
      import java.util.*;
      import java.util.regex.*;
      import org.apache.poi.hssf.usermodel.*;
      import org.apache.poi.poifs.filesystem.POIFSFileSystem;

      public class InsertExcelFileDataIntoDatabase {
      public static void main( String [] args ) {
        String fileName="test.xls";
        Vector dataHolder=read(fileName);
        saveToDatabase(dataHolder);
    }
        public static Vector read(String fileName)    {
        Vector cellVectorHolder = new Vector();
        try{
                FileInputStream myInput = new FileInputStream(fileName);
                POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
            HSSFSheet mySheet = myWorkBook.getSheetAt(0);
           Iterator rowIter = mySheet.rowIterator(); 
           while(rowIter.hasNext()){
                  HSSFRow myRow = (HSSFRow) rowIter.next();
                  Iterator cellIter = myRow.cellIterator();
                  Vector cellStoreVector=new Vector();
                  while(cellIter.hasNext()){
                          HSSFCell myCell = (HSSFCell) cellIter.next();
                          cellStoreVector.addElement(myCell);
                  }
                  cellVectorHolder.addElement(cellStoreVector);
          }
        }catch (Exception e){e.printStackTrace(); }
        return cellVectorHolder;
    }
        private static void saveToDatabase(Vector dataHolder) {
        String name="";
                String address="";
                for (int i=0;i<dataHolder.size(); i++){
                   Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
                        for (int j=0; j < cellStoreVector.size();j++){
                                HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
                                String st = myCell.toString();
                                 name=st.substring(0,1);
                                 address=st.substring(0);
                                                        }
                        try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
        Statement stat=con.createStatement();
        int k=stat.executeUpdate("insert into person(Name,Address) value('"+name+"','"+address+"')");
        System.out.println("Data is inserted");
        stat.close();
        con.close();
        }
        catch(Exception e){}
        }
        }
      }

Ads









Related Tutorials/Questions & Answers:
excel
excel  how to read columns values of excel file in java and store in array list in java like col1 col2 2 3 1 7 7 9 then list1 have values of col1 and list2 have values of col2...  
excel
excel  how to save excel sheet data into the database using poi api?   import java.io.*; import java.sql.*; import java.util.*; import java.util.regex.*; import org.apache.poi.hssf.usermodel.
Advertisements
JDBC to EXcel
JDBC to EXcel  How to insert data in excel sheet from java
Excel to database
Excel to database  Hi I want to update SQL database with the values from excel using java code. Can you help me
JAVA EXCEL
JAVA EXCEL  How to read the contents of an excel, perform some calculation and wrote the calculated values to another excel using poi
download excel
download excel  hi i create an excel file but i don't i know how to give download link to that excel file please give me any code or steps to give download link
JAVA_EXCEL
JAVA_EXCEL  I need to read the all the contents of excel which has... content another excel. For example if I have three tabs in an excel with some contents... to another excel. Please help me out with this problem
Sql to excel
Sql to excel  How to convert a Sql Data into excel Doc with Using Netbeans
JSP TO EXCEL
JSP TO EXCEL  Hi sir/mam, How to import data to excel using jsp without retrieving database.   friend, you can't import excel data into the middle of an HTML pages (your JSP will result in an HTML page
convert excel into pdf
convert excel into pdf  How to convert excel into pdf files
uploading and reading the excel daa
uploading and reading the excel daa  uploading the excel file not getting the data of the excel
To Generate Excel
With EXCEL in it has to open up how can i achieve this pls explain
excel to database
excel to database   How to export data from excel sheet to mysql database by using java with request parameter Here q=40 is a request parameter ..request parameter using i export the all data to database   <
Excel Column change
Excel Column change  How to change sequence of columns in Excel sheet through Java
struts2 excel downloads
struts2 excel downloads  hi friend, how to set Timestamps(Date+time) values to excel sheet cells
How to Unlock Excel Password
How to Unlock Excel Password  anyone help me how to recover lost Excel file password
ModuleNotFoundError: No module named 'excel'
ModuleNotFoundError: No module named 'excel'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'excel' How to remove the ModuleNotFoundError: No module named 'excel'
ModuleNotFoundError: No module named 'excel'
ModuleNotFoundError: No module named 'excel'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'excel' How to remove the ModuleNotFoundError: No module named 'excel'
ModuleNotFoundError: No module named 'excel'
ModuleNotFoundError: No module named 'excel'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'excel' How to remove the ModuleNotFoundError: No module named 'excel'
ModuleNotFoundError: No module named 'excel'
ModuleNotFoundError: No module named 'excel'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'excel' How to remove the ModuleNotFoundError: No module named 'excel'
how to use Excel Templet to write excel file using java.
how to use Excel Templet to write excel file using java.  how to use Excel Templet to write excel file using java
struts2 excel downloads
struts2 excel downloads  hi sir, am new to the real time environment. am going to create excel sheet and setting values to excel sheet from db table dynamically by using Hibernate persistence logic.......am using struts2
Excel Password Cracker Software
Excel Password Cracker Software  Hello I just want to know what is Excel Password Recovery software? I want need to recover a worksheet since I forgotten
how to use Excel Template to write excel file using java
how to use Excel Template to write excel file using java  How to use Excel template to write data in that file using java
excel uploading in jsp
excel uploading in jsp  could you provide the source code for: 1)have to upload an empty excel sheet at client side i.e if client clicks an excel icon an empty excel sheet should open 2)when they fill data in that sheet and click
currency symbols in excel
currency symbols in excel  How to add a currency symbol in Excel sheet? Please guide me
Export html data to excel
Export html data to excel  I am trying to export data from an html page to an excel sheet.Any advise
Reading an excel file into array
Reading an excel file into array  Hi, I'm trying to read in an excel file, search a column for containing key words (entered by a user) and then displaying the matching rows in a table. I'm fairly new to JavaScript. Can anyone
excel to ms access
excel to ms access  hello sir i am developing a application which requires extracting data from excel sheet and place in the ms access database using java programming for example: in excel sheet i have 2 columns and more than
How to export grid into excel
How to export grid into excel  Hi, i created a grid panel i have to export it to the excel. please help me by some sample code. thanks in advance. cool day dude
search for exact match in excel
search for exact match in excel  How to search for an exact match in excel and show data if the matched sting is found? Please tell me the script. Thanks in Advance
save data in excel
save data in excel  hello sir, i'm new to this field i want to know can i save data in excel by html forms input,means if any one fill up form online then it is save offline in excel how can i do it can you give me solution sir
save data in excel
save data in excel  hello sir, i'm new to this field i want to know can i save data in excel by html forms input,means if any one fill up form online then it is save offline in excel how can i do it can you give me solution sir
read data from Excel sheet
read data from Excel sheet  Hi, How to read data from an excel sheet using JSP. Thank you
convert html to excel using jsp
convert html to excel using jsp   i want to convert a html page into mcrosoft excel page using jsp.how i do
insert excel sheet into mysql as a table
insert excel sheet into mysql as a table  sir, i want to import an excel sheet into mysql5.0 database as in the table format using tomcat 6.0 by jsp
Version of com.aimilin>excel-bean dependency
List of Version of com.aimilin>excel-bean dependency
Version of com.jameskleeh>excel-builder dependency
List of Version of com.jameskleeh>excel-builder dependency
Version of com.socialmetrix>excel-templater dependency
List of Version of com.socialmetrix>excel-templater dependency
odbc MS EXCEL
odbc MS EXCEL  hey my whole database is stored in MS Excel. so please tell me that can we connect to that database. if yes, how to do that. please help me out i am doing it in netbeans can please tell me the steps
ModuleNotFoundError: No module named 'agate-excel'
ModuleNotFoundError: No module named 'agate-excel'  Hi, My Python... 'agate-excel' How to remove the ModuleNotFoundError: No module named 'agate-excel' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'agate-excel'
ModuleNotFoundError: No module named 'agate-excel'  Hi, My Python... 'agate-excel' How to remove the ModuleNotFoundError: No module named 'agate-excel' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'apyref-excel'
ModuleNotFoundError: No module named 'apyref-excel'  Hi, My Python... 'apyref-excel' How to remove the ModuleNotFoundError: No module named 'apyref-excel' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'apyref-excel'
ModuleNotFoundError: No module named 'apyref-excel'  Hi, My Python... 'apyref-excel' How to remove the ModuleNotFoundError: No module named 'apyref-excel' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'apyref-excel'
ModuleNotFoundError: No module named 'apyref-excel'  Hi, My Python... 'apyref-excel' How to remove the ModuleNotFoundError: No module named 'apyref-excel' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'apyref-excel'
ModuleNotFoundError: No module named 'apyref-excel'  Hi, My Python... 'apyref-excel' How to remove the ModuleNotFoundError: No module named 'apyref-excel' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'apyref-excel'
ModuleNotFoundError: No module named 'apyref-excel'  Hi, My Python... 'apyref-excel' How to remove the ModuleNotFoundError: No module named 'apyref-excel' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'automate-excel'
ModuleNotFoundError: No module named 'automate-excel'  Hi, My... named 'automate-excel' How to remove the ModuleNotFoundError: No module named 'automate-excel' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'autosphere-excel'
ModuleNotFoundError: No module named 'autosphere-excel'  Hi, My... named 'autosphere-excel' How to remove the ModuleNotFoundError: No module named 'autosphere-excel' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'capiq-excel'
ModuleNotFoundError: No module named 'capiq-excel'  Hi, My Python... 'capiq-excel' How to remove the ModuleNotFoundError: No module named 'capiq-excel' error? Thanks   Hi, In your python environment

Ads