使用Java插入Oracle数据库后行的顺序错误

时间:2017-01-30 16:38:48

标签: java excel oracle

这是我的代码:

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import java.util.*;
Dimport java.sql.*; 


public class xlsToOracle 
{  
    public static void main(String[] args) throws Exception
    {                

            Class.forName ("oracle.jdbc.OracleDriver"); 
            Connection conn = DriverManager.getConnection
    ("jdbc:oracle:thin:@//localhost:1521/xe", "SYSTEM", "Password");

            PreparedStatement sql_statement = null;
            String jdbc_insert_sql = "INSERT INTO XLS_POI"
                            + "(KEYWORD, TOTAL_COUNT) VALUES"
                            + "(?,?)";
            sql_statement = conn.prepareStatement(jdbc_insert_sql);

            FileInputStream input_document = new FileInputStream(new File("aaa.xls"));
            HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);
            HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);

            //Iterator<Row> rowIterator = my_worksheet.iterator(); 
            //String str;

            int i = 0, j = 0;

            Sheet sheet = my_xls_workbook.getSheet("Sheet1");
            int numRow = sheet.getLastRowNum();
            for (i = 0; i <= numRow; i++) 
            {
                Row row = sheet.getRow(i);
                if(row == null) 
                {
                    continue;
                }

                for (j = 0; j < 2; j++) 
                {
                    Cell cell = row.getCell(j);
                    if(cell == null) 
                    {
                        continue;
                    }
                    String val1 = row.getCell(j).toString();
                    sql_statement.setString(j+1, val1); 
                }

                sql_statement.executeUpdate(); 

            }

            input_document.close();
            sql_statement.close();
            conn.commit();
            conn.close();
    }
}

所以我想从excel表中获取数据并将其放入我的Oracle数据库中。此代码工作正常,但插入行后的顺序错误。最后插入两个第一行。见图片。

这是我在excel中的文档的样子: Excel Data

这就是它在数据库中的样子: DB Data

我做错了什么?

0 个答案:

没有答案