1

I am using this code to import values from an Excel file to my Access database. Everything is working well except for the fact that the code would read all the cells but won't copy everything into access table.

(Eg.-200 rows are read successfully but only 97 are entered into the database and the code stops without any error.)The description which is being entered here is about 100 words per row.

public class selector extends javax.swing.JFrame {

    final JFileChooser fc = new JFileChooser();
    File file;
    static String filename, query2, item;
    static String[][] itemss = new String[10000][10000];
    static double xxx;
    static datacon dc = new datacon();
    public static Statement st;

    static int i, j, rows = -1, p;

        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            try {
                // TODO add your handling code here:
                fileChose();
            } catch (SQLException ex) {
                Logger.getLogger(selector.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(selector.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (InvalidFormatException ex) {
            Logger.getLogger(selector.class.getName()).log(Level.SEVERE, null, ex);
        }

    }                                        
    public static String fileChose() throws InvalidFormatException, SQLException, FileNotFoundException {
        JFileChooser fc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("XLS files", "xls", "XLSX files", "xlsx");
        fc.setFileFilter(filter);
        int ret = fc.showOpenDialog(null);

        if (ret == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();

            filename = file.getAbsolutePath();
            System.out.println(filename);

            work();
            return filename;
        } else {
            return null;
        }
    }

    static void work() throws InvalidFormatException, SQLException, FileNotFoundException {
        try {
            FileInputStream file = new FileInputStream(new File(filename));

            org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(file);
            org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            dc.connect();

            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                rows++;
                i = 0;
                //For each row, iterate through all the columns 
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell;
                    cell = cellIterator.next();

                    //Check the cell type and format accordingly 
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            xxx = cell.getNumericCellValue();
                            itemss[rows][i] = Double.toString(xxx);
                            break;
                        case Cell.CELL_TYPE_STRING:
                            item = "" + cell.getRichStringCellValue();
                            itemss[rows][i] = item;
                            break;
                        case Cell.CELL_TYPE_BLANK:
                            item = "" + cell.getRichStringCellValue();
                            itemss[rows][i] = item;
                            break;
                    }
                    System.out.println("coloumn " + i + " : " + itemss[rows][i]);
                    i++;
                }
            }
            file.close();

            for (j = 0; j < rows; j++) {
                String query = " INSERT INTO schedule ([counter],[description]) VALUES ('" + j + "','" + itemss[j][1] + "') ";
                st.executeUpdate(query);
                System.out.println(query);
            }

        } catch (Exception e) {
        }
    }
public static class datacon {

        public Connection con;
        // public Statement st;
        public ResultSet rs;

        public void connect() {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:database4");
                st = con.createStatement();

            } catch (Exception t) {
                System.out.println(t.toString());
            }

        }
    }
}

Image

4
  • what is the value of rows before the for (j = 0; j < rows; j++) { loop? Commented Jul 1, 2014 at 11:00
  • @NikhilTalreja Its 261 for my file Commented Jul 1, 2014 at 11:04
  • Check if size of rows matches with the no. of elements in itemss Commented Jul 1, 2014 at 11:08
  • @NikhilTalreja no of elements matches with rows Commented Jul 1, 2014 at 11:13

2 Answers 2

1

Your Insert query may be throwing exceptions. You are not logging them:

    for (j = 0; j < rows; j++) {
            String query = " INSERT INTO schedule ([counter],[description]) VALUES ('" + j + "','" + itemss[j][1] + "') ";
            st.executeUpdate(query);
            System.out.println(query);
        }

    } catch (Exception e) {
        //no logging
    }

Also work() has declared exceptions that it can throw and yet the whole implementation is in try catch

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks...You are right. This is the error (java.sql.SQLException: General error at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6993) at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6993) at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121) at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3117) at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337) at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:287)
Thanks a lot.....I was not able to work this out for the last 7 days. Your prompt response really helped. My code is working fine now.
0

Probably you have a problem in your switch code.

Add a default value and check if the code pass in this part and what is the cell type.

something like this:

default:
    System.out.println("Default case: "+ cell.getCellType());

2 Comments

Actually everything is correctly stored in the itemss[][] string and this default case is not being executed anywhere. The problem is with its insertion into access table.
you have a st.commit() function? try it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.