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?