java - SQL : Vehicule gets added to DataBase but not his options

标签 java sql jdbc hsqldb

这道题是这道题的延续:

Click here

我不再抛出任何异常。数据被添加到 vehicule 表,但没有添加到 vehicule_option 表。

你知道为什么吗?

public boolean create(Vehicule v){
    String query = "INSERT INTO vehicule (MARQUE, MOTEUR, PRIX, NOM) VALUES (";
    query += v.getMarque().getId() + ", "
            + v.getMoteur().getId() + ", "
            + v.getPrix() + ", \'"
            + v.getNom() + "\');";

    try{
        connect.setAutoCommit(false);
        ResultSet nextID = connect.prepareStatement("CALL NEXT VALUE FOR seq_vehicule_id").executeQuery();
        if (nextID.next()){
            int ID = nextID.getInt(1);
            v.setId(ID-1);
        }

        for (Option o : v.getOptions()){
            System.out.println(v.getId() + " " + o.getId());
            query += "INSERT INTO vehicule_option (id_vehicule, id_option) VALUES ("
                    + v.getId() + ", " + o.getId() + ");";
        }

        Statement state = this.connect.createStatement();
        ResultSet rs = state.executeQuery(query);

    } catch (SQLException e){
        e.printStackTrace();
    }

    return true;
}

这是我使用此代码获得的查询示例:

INSERT INTO vehicule (MARQUE, MOTEUR, PRIX, NOM) VALUES (0, 0, 250.0, 'test');
INSERT INTO vehicule_option (id_vehicule, id_option) VALUES (34, 0);
INSERT INTO vehicule_option (id_vehicule, id_option) VALUES (34, 1);
INSERT INTO vehicule_option (id_vehicule, id_option) VALUES (34, 2);
INSERT INTO vehicule_option (id_vehicule, id_option) VALUES (34, 3);
INSERT INTO vehicule_option (id_vehicule, id_option) VALUES (34, 4);

最佳答案

请在下面找到更新的代码。查看内联评论:

public boolean create(Vehicule v) {
        String query = "INSERT INTO vehicule (MARQUE, MOTEUR, PRIX, NOM) VALUES (";
        query += v.getMarque().getId() + ", " + v.getMoteur().getId() + ", "
                + v.getPrix() + ", \'" + v.getNom() + "\')";

        try {
            connect.setAutoCommit(false);
            ResultSet nextID = connect.prepareStatement(
                    "CALL NEXT VALUE FOR seq_vehicule_id").executeQuery();
            if (nextID.next()) {
                int ID = nextID.getInt(1);
                v.setId(ID - 1);
            }

            /* Save vehicle */
            Statement state = this.connect.createStatement();
            state.executeUpdate(query);

            /* Now start with creating query for vehicle_option. Execute only if collection not empty */
            if (!v.getOptions().isEmpty()) {
                query = "INSERT INTO vehicule_option (id_vehicule, id_option) VALUES ";
                boolean valueAdded = false;
                for (Option o : v.getOptions()) {
                    System.out.println(v.getId() + " " + o.getId());
                    if (valueAdded) {
                        query += ", ";
                    }
                    query += "(" + v.getId() + ", " + o.getId() + ")";
                    valueAdded = true;
                }

                state = this.connect.createStatement();
                state.executeUpdate(query);
            }
            /* Commit inserts if all queries executed fine */
            connect.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return true;
    }

建议您将字符串连接替换为 StringBuilder 以构建查询。

关于java - SQL : Vehicule gets added to DataBase but not his options,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32923200/

相关文章:

java - 我现在正在使用 SAX 进行解析

java - DailyRollingFileHandler ---- 文件应每天轮换

sql - 从sql中提取字符详细信息的特定位置

java - 在 PreparedStatement 中使用 setDate

java - 如何解决MySQLSyntaxErrorException异常?

java - 创建表时出现 MySQL 语法错误 - Java

java - 从 REST 访问请求对象

sql - 谷歌大查询 : Forward Filling: IGNORE in Window Function

sql - 如何一次最小化所有 SQL 存储过程

Hibernate:javax.persistence.* 驱动程序属性导致 UnsupportedOperationException:用户必须提供 JDBC 连接?