简体   繁体   English

我的第二张表没有填充数据

[英]My second table isn't being filled with data

Alright.. So I've made a small application which makes a connection to my database. 好吧..所以我做了一个小应用程序,它连接到我的数据库。 I can read out an excel file and store those files into an array. 我可以读出一个excel文件并将这些文件存储到一个数组中。 My database has multiple tables. 我的数据库有多个表。 I can fill in one table, but when I want to fill in the other table aswell, it won't work. 我可以填写一张桌子,但是当我想填写另一张桌子时,它也行不通。 I get a load of red lines in my stack trace and I can't figure out what to do.. So I came here desperately in hope of some help. 我的堆栈跟踪中出现了一堆红线,我无法弄清楚要做什么..所以我拼命地来到这里,希望得到一些帮助。

My program consists of a Main: 我的程序包含一个Main:

public static void main(String[] args){
    CustomerMC cmc = new CustomerMC();
    ArrayList<Customer> customers = new ArrayList<>();
    ArrayList<Order> orders = new ArrayList<>();
    ArrayList<ArrayList> gay = 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);
                    gay.add(customers);
                    gay.add(orders);
                }


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

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



}

Two container classes: 两个容器类:

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

My model class customer with queries and such: 我的模型类客户有查询等:

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";

    }

    return returning;

}
public String insertOrder(String orderid, String product, String id) {
    String returning = null;
    try {
        query = "insert into Order 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) {
        System.out.println();
        e.printStackTrace();

    }

    return returning;

}

The first class; 第一堂课; which is the customer table fills in perfectly fine. 这是客户表填写完全正常。 It prints out what it has to do. 它打印出它必须做的事情。 However, the order table, second one, doesn't. 但是,订单表,第二个,没有。 The output I get for that is as following: 我得到的输出如下:

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
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:65)

at p4project.Main.main(Main.java:102)
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order values(2,'Couch',1)' at line 1

I hope someone can help, any help is welcome. 我希望有人能提供帮助,欢迎任何帮助。

Sincerely, 此致

Double

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order values(2,'Couch',1)' at line 1 检查与MySQL服务器版本对应的手册,以便在第1行的“订单值(2,'Couch',1)'附近使用正确的语法

Order is a reserved word for MySQL so instead you have to put the name of your table between `` like this: Order是MySQL的保留字,所以你必须把你的表名放在``喜欢这个:

query = "insert into `Order` values(?, ?, ?);";
//-------------------^-----^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM