在数据库中插入冗余数据

我有一个任务从Excel工作表中获取数据,并在MySQL数据库中进行更新,该数据库有2列USN和Name。 问题是USN正在打印行和名称。 例如,如果我将其保存在数据库中时有12条logging,则会插入24条logging。 我也在使用Swing的概念。 以下是我的代码。

class OpenClass implements ActionListener { public void actionPerformed(ActionEvent e) { Workbook w ; chooser = new JFileChooser(); JFileChooser chooser = new JFileChooser(); int option = chooser.showOpenDialog(ExcelFileUploading.this); if(option == JFileChooser.APPROVE_OPTION) { try { w = Workbook.getWorkbook(chooser.getSelectedFile()); Sheet sheet = w.getSheet(0); for(int i = 0; i<sheet.getRows(); i++) { for(int j=0; j<sheet.getColumns();j++) { Cell cell = null; if(j==0) { cell = sheet.getCell(j, i); if(checkIfNumber(cell.getContents().toString())== false) { //System.out.print("\t\t"); continue; } System.out.print("\n"); } cell = sheet.getCell(j, i); CellType type = cell.getType(); if((type == CellType.LABEL)|| (type == CellType.NUMBER)) { try { Class.forName(driver); con = DriverManager.getConnection(url+dbName,username,password); stmt = con.createStatement(); String query = "INSERT INTO student (USN,Name)" +"VALUES('"+cell.getContents()+"','"+cell.getContents()+"')"; stmt.executeUpdate(query); } catch (Exception exp) { exp.printStackTrace(); } finally { try { stmt.close(); con.close(); } catch (SQLException exp) { } } } } } } catch (Exception exp) { exp.printStackTrace(); } display.setText("Opened File : " +((chooser.getSelectedFile()!=null)? chooser.getSelectedFile().getName(): "nothing")); } if(option == JFileChooser.CANCEL_OPTION) { display.setText("Open Operation Cancelled"); } } private boolean checkIfNumber(String string) { try { double d = Double.parseDouble(string); //System.out.print(d); return true; } catch (NumberFormatException ne) { return false; } } } 

由于在我的Db表(USN,名称)中有2列,我不得不放置cell.getContents()两次。

谁能帮我?

你遍历Excel中的所有行,然后遍历Excel中的所有列。 你使用2列你不需要第二个循环。 此外,您只有一个Cell对象,并且只保留一个单元的引用,但是当您创build插入语句时,您将为SQL中的两个值分配相同的单元

所以如果你有2行(用“,”字符分隔的列:

  • 1,11
  • 2,22

你的SQL语句是:

 INSERT INTO student (USN,Name)VALUES('1','1') INSERT INTO student (USN,Name)VALUES('11','11') INSERT INTO student (USN,Name)VALUES('2','2') INSERT INTO student (USN,Name)VALUES('12','12') 

你想要的是这样的,在每一行我们:

  1. 检查第一个单元格,看看它是否是一个string,如果是的话,分配给cell1。
  2. 检查第二个单元格,看它是否合适的types。 转到下一行,如果没有,则转到步骤1。
  3. 使用Cell1和Cell2创buildSQL语句。

所以,没有实际的mysql连接/插入我想你更像这样的东西:

 public class OpenClass { public static void main(String args[]) { new OpenClass().testIt(); } public void testIt() { Workbook w; JFileChooser chooser = new JFileChooser(); int option = chooser.showOpenDialog(null); //CREATE TWO CELL OBJECTS Cell cell1, cell2; if (option == JFileChooser.APPROVE_OPTION) { try { w = Workbook.getWorkbook(chooser.getSelectedFile()); Sheet sheet = w.getSheet(0); //ITERATE BY ROWS for (int i = 0; i < sheet.getRows(); i++) { //GET AND CHECK COLUMN1 VALUE cell1 = sheet.getCell(0, i); if (checkIfNumber(cell1.getContents().toString()) == false) continue; //GET CHECK COLUMN2 VALUE cell2 = sheet.getCell(1, i); CellType type = cell2.getType(); if ((type == CellType.LABEL) || (type == CellType.NUMBER)) { String query = "INSERT INTO student (USN,Name)" + "VALUES('" + cell1.getContents() + "','" + cell2.getContents() + "')"; System.out.println(query); //RUN YOUR QUERY HERE! } } } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private boolean checkIfNumber(String string) { try { double d = Double.parseDouble(string); //System.out.print(d); return true; } catch (NumberFormatException ne) { return false; } } } 

最后,由于扩展开销,连接只能创build一次到数据库。 jexcelapi库代码与您的代码完美协作,因此您必须使用该库。