2017-06-02 70 views
-1

我做了一個讀取Excel文件的小應用程序。我將它連接到我的MySQL數據庫,以便我可以將該文件放入包含多個表的數據庫中。但是,在將值實施到「訂單」表中時,存在SQLException,並且這些值不會進入數據庫。這是CustomerMC類中的例外情況。 「客戶」表的值確實進入數據庫。如何閱讀Excel工作表的一部分,而不是使用Java整體?

我所擁有的代碼包含一個主要位置,我讀取文件並創建Customer和Order實例,兩個值進入的容器類,一個與此無關的連接類,以及一個Model類客戶查詢所在的位置。

主營:

public static void main(String[] args){ 
    CustomerMC cmc = new CustomerMC(); 
    ArrayList<Customer> customers = new ArrayList<>(); 
    ArrayList<Order> orders = new ArrayList<>(); 

    try { 
     try (FileInputStream file = new FileInputStream(new File("C:/Users/Wout/Desktop/ProjectTest.xlsx"))) { 
      XSSFWorkbook wb = new XSSFWorkbook(file); 
      XSSFSheet sh = wb.getSheetAt(0); 

      Iterator<Row> rowIterator = sh.iterator(); 
      while(rowIterator.hasNext()) { 

       Row row = rowIterator.next(); 
       Iterator<Cell> cellIterator = row.cellIterator(); 

       int counter = 0; 
       int cid = 0; 
       int OrderID = 0; 
       String name = ""; 
       String purchase = ""; 
       String age = ""; 
       String product = ""; 

       while (cellIterator.hasNext() && row.getRowNum() != 0) { 
        Cell cell = cellIterator.next(); 
        counter++; 

        switch (counter) { 
         case 1: 
          cid = (int)cell.getNumericCellValue(); 
          break; 
         case 2: 
          name = cell.getStringCellValue(); 
          break; 
         case 3: 
          purchase = cell.getStringCellValue(); 
          break; 
         case 4: 
          age = "" + cell.getNumericCellValue(); 
          break; 
         case 5: 
          OrderID = (int)cell.getNumericCellValue(); 
          break; 
         case 6: 
          product = "" + cell.getStringCellValue(); 
          break; 
         case 7: 
          cid = (int)cell.getNumericCellValue(); 
          break; 
        } 
       } 
       if(row.getRowNum() != 0) { 
        Customer customer = new Customer(cid,name,purchase,age); 
        Order order = new Order(OrderID, product, cid); 
        customers.add(customer); 
        orders.add(order); 
       } 


      } 
     } 
    } catch (FileNotFoundException ex) { 
     System.out.println("File has not been found"); 
    } catch (IOException ex) { 
     System.out.println("IOException"); 
    } 

    for(Order order : orders) { 
     System.out.println(order.toString()); 
     cmc.insertOrder(""+order.getOrderid(), order.getProduct(), ""+order.getCid()); 
    } 

    for(Customer customer : customers){ 
     System.out.println(customer.toString()); 
     cmc.insertCustomer(""+customer.getCid(), customer.getName(), customer.getPurchase(), customer.getAge());   
    } 


} 

容器類:

public class Customer { 
private int cid; 
private String name; 
private String purchase; 
private String age; 

public Customer(int cid, String name, String purchase, String age) { 
    this.cid = cid; 
    this.name = name; 
    this.purchase = purchase; 
    this.age = age; 
} 

public int getCid() { 
    return cid; 
} 

public void setCid(int cid) { 
    this.cid = cid; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getPurchase() { 
    return purchase; 
} 

public void setPurchase(String purchase) { 
    this.purchase = purchase; 
} 

public String getAge() { 
    return age; 
} 

public void setAge(String age) { 
    this.age = age; 
} 

@Override 
public String toString() { 
    return "" + cid + " ; "+ name + " ; " + purchase + " ; " + age; 
} 

public class Order { 
private int orderid; 
private String product; 
private int cid; 

public Order(int orderid, String product, int cid) { 
    this.orderid = orderid; 
    this.product = product; 
    this.cid = cid; 
} 

public int getOrderid() { 
    return orderid; 
} 

public void setOrderid(int orderid) { 
    this.orderid = orderid; 
} 

public String getProduct() { 
    return product; 
} 

public void setProduct(String product) { 
    this.product = product; 
} 

public int getCid() { 
    return cid; 
} 

public void setCid(int cid) { 
    this.cid = cid; 
} 

@Override 
public String toString() { 
    return "" + orderid + " ; " + product + " ; " + cid; 
} 

客戶模型類:

public class CustomerMC { 
private Connection connection; 
private PreparedStatement pst; 
String query; 
ResultSet rs; 

public CustomerMC(){ 
    try{ 
     connection = SimpleDataSourceV2.getConnection(); 
    } 
    catch(SQLException e){ 
     System.out.println("Connection failure"); 
     e.printStackTrace(); 
    } 
} 

public String insertCustomer(String cid, String name, String purchase, String age) { 
    String returning = null; 
    try { 
     query = "insert into CustomerService values(?,?,?,?);"; 

     pst = connection.prepareStatement(query); 
     pst.setInt(1, Integer.parseInt(cid)); 
     pst.setString(2, name); 
     pst.setString(3, purchase); 
     pst.setString(4, age); 

     int response = pst.executeUpdate(); 
     returning = response +" Records has/have been edited"; 

    } catch (SQLException e) { 
     returning = "An error has occured"; 
     System.out.println(returning); 

    } 

    return returning; 

} 
public String insertOrder(String orderid, String product, String id) { 
    String returning = null; 
    try { 
     query = "insert into CustomerService values(?,?,?,?);"; 

     pst = connection.prepareStatement(query); 
     pst.setInt(1, Integer.parseInt(orderid)); 
     pst.setString(2, product); 
     pst.setInt(3, Integer.parseInt(id)); 

     int response = pst.executeUpdate(); 
     returning = response +" Records has/have been edited"; 

    } catch (SQLException e) { 
     returning = "xx"; 
     System.out.println(returning); 

    } 

    return returning; 

} 

顯然還有一個類連接到數據庫。這與這個問題無關,所以我決定放棄它。

然後我的Excel工作表是一個簡單的工作表,其中cid,姓名,購買者和年齡是'客戶'表和訂單ID,產品和cid是'訂單'表。訂單中的Cid是客戶的外鍵。 Excel會自動生成該字段作爲它在customers表中獲取的值。我重新實現了cid值,使我更容易。

表:https://prnt.sc/ff1yd0

我運行後得到的輸出如下:

 
    1 ; Monitor ; 1 
    java.sql.SQLException: Column count doesn't match value count at row 1 
    2 ; Couch ; 2 
    3 ; Appartment ; 3 
     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) 
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) 
     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) 
     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) 
     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) 
     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794) 
     at 
    com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) 
     at 
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) 
     at 
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375) 
     at 
    com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) 
     at p4project.CustomerMC.insertOrder(CustomerMC.java:66) 
     at p4project.Main.main(Main.java:103) 
    java.sql.SQLException: Column count doesn't match value count at row 1

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)

我不知道爲什麼第二個表沒有實現,我希望有人可以幫忙!任何幫助表示讚賞。

+0

你應該發佈你看到的異常。 – lucasvw

+0

用異常的堆棧跟蹤更新問題。你應該打印堆棧軌跡而不是'xx',它不會告訴你什麼是錯誤的 – lucasvw

+0

https://prnt.sc/ff25z7 ^這是堆棧跟蹤的輸出。你看到的錯誤線以後也會重複。不可能一蹴而就。 – Wout

回答

1

您的例外情況引發:CustomerMC.insertOrder(CustomerMC.java:66)並且說 java.sql.SQLException: Column count doesn't match value count

你的CustomerService表似乎有7列,但你想插入只有4個值的行。這不可能。您必須爲每列提供值(如果您未指定要插入值的列的名稱)。如果模式允許,也可以插入空值,但必須指定它。該聲明不知道你想留空的列。

+0

我剛剛注意到我的文件中有一個錯誤..我解決了這個問題,但我偶然發現了另一個錯誤。問題將被編輯。 – Wout

+0

一般來說:如果您還有其他問題(與第一個問題不太相近),請寫一個新問題。否則,我的答案會使原問題的背景變得鬆散。 – Simulant

+0

請問,已經做到了.. – Wout

相關問題