Import Excel table content into MySQL database using Java technology

Source: Internet
Author: User

1. Add the POI jar package to the project's Lib directory ­
2. Excel file directory:d://excel.xls­
3. Database field: Num1 num2 num3 num4 num5 num6­
4. Database name:blog­
5. Table name:test­
6. Writing class: String method to connect MySQL, insert method, entity class ­­,

 

import java.io.fileinputstream;&shy;import java.io.filenotfoundexception;&shy;import  java.io.ioexception;&shy;import org.apache.commons.logging.log;&shy;import  org.apache.commons.logging.logfactory;&shy;import org.apache.poi.hssf.usermodel.hssfcell;&shy; Import org.apache.poi.hssf.usermodel.hssfrow;&shy;import org.apache.poi.hssf.usermodel.hssfsheet ; &shy;import org.apache.poi.hssf.usermodel.hssfworkbook;&shy;public class testexcel  {&shy;      //output information for the record class &shy;       Static log log = logfactory.getlog (Testexcel.class); &shy;       //gets the path to the Excel document &shy;      public static string  filePath =  "D://excel.xls"; &shy;      public static  void main (String[] args)  {&shy;            try {&shy;                   //  Create a reference to an Excel workbook file &shy;                   hssfworkbook wookbook = new hssfworkbook (New FileInputStream ( FilePath));&shy;                    //  in an Excel document, the default index of the first sheet is 0                   //  its statement is:hssfsheet sheet =  Workbook.getsheetat (0);&shy;                   hssfsheet sheet = wookbook.getsheet ("Sheet1");&shy;                   //gets the number of lines in the Excel file &shy;                   int rows  = sheet.getphysicalnumberofrows ();                   //traversing rows &shy;                   for  (int i = 0; i  < rows; i++)  {&shy;                         //  read upper left cell &shy;                          hssfrow row = sheet.getrow (i);&shy;                         //  line is not empty & shy;                         if  (row != null)  {&shy;                                //get all the columns in the Excel file &shy;                                int cells = row.getphysicalnumberofcells (); &shy;                               string value =   "";     &shy;                               //traversing Columns &shy;                                for  (int j = 0; j  < cells; j++)  {&shy;                                      //gets the value of the column &shy;                                      hssfcell cell = row.getcell (j); &shy;                                     if  (cell !=  null)  {&shy;                                            switch  (Cell.getcelltype ())  {& shy;                                                  case hssfcell.cell_type_ formula:&shy;                                                  break;&shy;                                                   case HSSFCell.CELL_TYPE_NUMERIC:&shy;                                                         value +=  Cell.getnumericcellvalue ()  +  ",";        &shy;                                                   break;  &shy;                                                   case HSSFCell.CELL_TYPE_STRING:&shy;                                                         value +=  Cell.getstringcellvalue ()  +  ","; &shy;                                                  break;&shy;                                                   default:&shy;                                                         value +=  "0";&shy;                                                   break;&shy;                                      }&shy;                                }                               }& shy;                         //  inserting data into the MySQL database &shy;                         String[] val =  Value.split (",");&shy;                         TestEntity entity = new  Testentity ();&shy;               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM1 (val[0]);&shy;                          entity.setnum2 (val[1]);&shy;           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM3 (val[2]); &shy;                       &NBSP;&NBSP;ENTITY.SETNUM4 (val[3]);&shy;            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM5 (Val[4]);&shy;                    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ENTITY.SETNUM6 (val[5]);&shy;                          Testmethod method = new testmethod ();&shy;                         method. ADD (Entity);&shy;                   }&shy;             }&shy;       } catch  (filenotfoundexception e)  {&shy;             e.printstacktrace ();&shy;       } catch  (ioexception e)  {&shy;             e.printstacktrace ();&shy;      }&shy;    }&shy;} &shy;

Import Excel table content into MySQL database using Java technology

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.