从Excel中插入值以使用Java访问数据库

我正在使用此代码从Excel文件导入到我的Access数据库的值。 除了代码将读取所有单元,但不会将所有内容都复制到访问表中的事实之外,一切正常。

(例如,200行被成功读取,但只有97行被input到数据库中,并且代码停止而没有任何错误。)这里input的描述是每行大约100个字。

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

图片

您的插入查询可能会引发exception。 你没有logging他们:

  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 } 

另外work()已经声明了它可以抛出的exception,但是整个实现仍然在try catch中

您的交换机代码可能有问题。

添加一个默认值,并检查代码是否通过了这个部分,单元格types是什么。

像这样的东西:

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