Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 泰德。。!"); //我们循环并插入数据 迭代器ite=sheet.rowditerator(); System.out.println(“调用了行迭代器…”); while(ite.hasNext()){ Row Row=(Row)ite.next();_Java_Mysql_Excel_Apache_Apache Poi - Fatal编程技术网

Java 泰德。。!"); //我们循环并插入数据 迭代器ite=sheet.rowditerator(); System.out.println(“调用了行迭代器…”); while(ite.hasNext()){ Row Row=(Row)ite.next();

Java 泰德。。!"); //我们循环并插入数据 迭代器ite=sheet.rowditerator(); System.out.println(“调用了行迭代器…”); while(ite.hasNext()){ Row Row=(Row)ite.next();,java,mysql,excel,apache,apache-poi,Java,Mysql,Excel,Apache,Apache Poi,泰德。。!"); //我们循环并插入数据 迭代器ite=sheet.rowditerator(); System.out.println(“调用了行迭代器…”); while(ite.hasNext()){ Row Row=(Row)ite.next(); System.out.println(“获取的行值…”); 迭代器cellIterator=row.cellIterator(); System.out.println(“调用了单元格迭代器…”); int指数=1; while(cellIt

泰德。。!"); //我们循环并插入数据 迭代器ite=sheet.rowditerator(); System.out.println(“调用了行迭代器…”); while(ite.hasNext()){ Row Row=(Row)ite.next(); System.out.println(“获取的行值…”); 迭代器cellIterator=row.cellIterator(); System.out.println(“调用了单元格迭代器…”); int指数=1; while(cellIterator.hasNext()){ Cell=cellIterator.next(); System.out.println(“获取单元格值…”); 开关(cell.getCellType()){ case Cell.Cell\u TYPE\u STRING://句柄字符串列 setString(索引,cell.getStringCellValue()); 打破 case Cell.Cell\u TYPE\u NUMERIC://处理双精度数据 int i=(int)cell.getNumericCellValue(); setInt(index,(int)cell.getNumericCellValue()); 打破 } 索引++; } //我们可以在读取下一行之前执行该语句 prepsmt.executeUpdate(); } /*关闭输入流*/ fis.close(); /*精心准备的声明*/ prepsmt.close(); /*密切联系*/ con.close(); }捕获(例外e){ e、 printStackTrace(); } } } 我的excel如下所示 101部手机10 102电池20 21 22 20 56 103手表25242525528 104个键盘10 11 12 12 105支钢笔55 3 -----------------------------------> 在我的数据库中,它按此方向保存[---------------->]
我无法将exell文件信息存储到数据库表中。这不是一个有效的声明。请向我们显示您面临的错误或当前输出?我得到了答案#San谢谢在下面发布答案您的问题可能会帮助其他人
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.*;
import java.sql.*; 

public class XLToDB {  

    public static final String INSERT_RECORDS = "INSERT INTO RECORDS" + "(SNO, ID, NAME, VALUE1, VALUE2, VALUE3, VALUE4, VALUE5) VALUES" + "(?,?,?,?,?,?,?,?)";
    private static String GET_COUNT = "SELECT COUNT(*) FROM RECORDS";

    public static void main(String[] args) throws Exception{
        XLToDB obj = new XLToDB();
        obj.insertRecords("E:/test.xlsx");



    }
          public void insertRecords(String filePath){

            /* Create Connection objects */
        Connection con = null;
        PreparedStatement prepStmt = null;
        java.sql.Statement stmt = null;
        int count = 0;
        ArrayList<String> mylist = new ArrayList<String>();

        try{
            con = DBHelper.getConnection();
            System.out.println("Connection :: ["+con+"]");
            prepStmt = con.prepareStatement(INSERT_RECORDS);
            stmt = con.createStatement();
            ResultSet result = stmt.executeQuery(GET_COUNT);
            while(result.next()) {

                int val = result.getInt(1);
                System.out.println(val);
                count = val+1;

            }


            prepStmt.setInt(1,count);

            /* We should now load excel objects and loop through the worksheet data */
            FileInputStream fis = new FileInputStream(new File(filePath));
             /* Load workbook */
            XSSFWorkbook workbook = new XSSFWorkbook (fis);
            /* Load worksheet */
            XSSFSheet sheet = workbook.getSheetAt(0);
               // we loop through and insert data
            Iterator ite = sheet.rowIterator();

               while(ite.hasNext()) {
                        Row row = (Row) ite.next(); 
                        Iterator<Cell> cellIterator = row.cellIterator();
                                while(cellIterator.hasNext()) {
                                        Cell cell = cellIterator.next();
                                      switch(cell.getCellType()) { 
                                        case Cell.CELL_TYPE_STRING: //handle string columns
                                                prepStmt.setString(1, cell.getStringCellValue());                                                                                     
                                                break;
                                        case Cell.CELL_TYPE_NUMERIC: //handle double data
                                                prepStmt.setDouble(2,cell.getNumericCellValue() );
                                                break;
                                        }                                       
                                }
                //we can execute the statement before reading the next row
                prepStmt.executeUpdate();
                }
               /* Close input stream */
               fis.close();
               /* Close prepared statement */
               prepStmt.close();
               /* COMMIT transaction */
               con.commit();
               /* Close connection */
               con.close();

        }catch(Exception e){
            e.printStackTrace();            
        }

        }
}
    import java.io.*;

    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    import java.util.*;
    import java.sql.*; 

    public class XLToDB {  

        public static final String INSERT_RECORDS = "INSERT INTO RECORDS(ID, NAME, VALUE1, VALUE2, VALUE3, VALUE4, VALUE5) VALUES(?,?,?,?,?,?,?)";
        private static String GET_COUNT = "SELECT COUNT(*) FROM RECORDS";

        public static void main(String[] args) throws Exception{
            XLToDB obj = new XLToDB();
            obj.insertRecords("E:/test.xlsx");



        }
              public void insertRecords(String filePath){

                /* Create Connection objects */
            Connection con = null;
            PreparedStatement prepStmt = null;
            java.sql.Statement stmt = null;
            int count = 0;
            ArrayList<String> mylist = new ArrayList<String>();

            try{
                con = DBHelper.getConnection();
                System.out.println("Connection :: ["+con+"]");
                prepStmt = con.prepareStatement(INSERT_RECORDS);
                stmt = con.createStatement();
                ResultSet result = stmt.executeQuery(GET_COUNT);
                while(result.next()) {

                    int val = result.getInt(1);
                    System.out.println(val);
                    count = val+1;

                }


                //prepStmt.setInt(1,count);

                /* We should now load excel objects and loop through the worksheet data */
                FileInputStream fis = new FileInputStream(new File(filePath));
                System.out.println("FileInputStream Object created..! ");
                 /* Load workbook */
                XSSFWorkbook workbook = new XSSFWorkbook (fis);
                System.out.println("XSSFWorkbook Object created..! ");
                /* Load worksheet */
                XSSFSheet sheet = workbook.getSheetAt(0);
                System.out.println("XSSFSheet Object created..! ");
                   // we loop through and insert data
                Iterator ite = sheet.rowIterator();
                System.out.println("Row Iterator invoked..! ");

                   while(ite.hasNext()) {
                            Row row = (Row) ite.next(); 
                            System.out.println("Row value fetched..! ");
                            Iterator<Cell> cellIterator = row.cellIterator();
                            System.out.println("Cell Iterator invoked..! ");
                            int index=1;
                                    while(cellIterator.hasNext()) {

                                            Cell cell = cellIterator.next();
                                            System.out.println("getting cell value..! ");

                                            switch(cell.getCellType()) { 
                                            case Cell.CELL_TYPE_STRING: //handle string columns
                                                    prepStmt.setString(index, cell.getStringCellValue());                                                                                     
                                                    break;
                                            case Cell.CELL_TYPE_NUMERIC: //handle double data
                                                int i = (int)cell.getNumericCellValue();
                                                prepStmt.setInt(index, (int) cell.getNumericCellValue());
                                                    break;
                                            }
                                            index++;



                                    }
                    //we can execute the statement before reading the next row
                    prepStmt.executeUpdate();
                    }
                   /* Close input stream */
                   fis.close();
                   /* Close prepared statement */
                   prepStmt.close();

                   /* Close connection */
                   con.close();

            }catch(Exception e){
                e.printStackTrace();            
            }

            }
    }


my excel is like below

101 Mobiles 10  10  10  10  10
102 Batteries   20  21  22  20  56
103 Watches 25  242 52  55  28
104 Keyboards   10  11  12  12  12
105 Pens    5   5   5   5   3
----------------------------------->
in my database it saves  in this direction[--------------------->>>]