0

This is my code:

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();
    }
}

So I want to get data from excel sheet and put it into my Oracle database. This code works fine, but after inserting rows are in the wrong order. Two first rows are inserted on the end. See images.

This is how my document in excel looks: Excel Data

And this how it looks in the database: DB Data

What am I doing wrong?

Community
  • 1
  • 1
westman379
  • 493
  • 1
  • 8
  • 25
  • 4
    There is no "wrong" order. Data isn't stored in any particular order in the database. It's a set of data, not an ordered set. If you want it in a particular order in your result set, you have to order it yourself, with an `order by` clause. [Here's a well-known treatise on the subject](http://tkyte.blogspot.co.uk/2005/08/order-in-court.html). – Alex Poole Jan 30 '17 at 16:39
  • @AlexPoole. Why not make this an Answer vs a comment? – BobC Jan 30 '17 at 16:43
  • @BobC - because I'm sure it's been answered before *8-) Hopefully the one I found explains the OP's situation well enough. – Alex Poole Jan 30 '17 at 16:44

0 Answers0