仅一次将值添加到数据库表

时间:2019-06-04 22:06:12

标签: java spring model-view-controller

我必须在Spring MVC中创建购物车应用程序。 我在数据库中有这个cartentries表,在其中存储book_id,id_cartEntry和其他字段。另外,我还有一个用户表,用于存储id_user。我的支架上有类似的条目,当我单击“结帐”按钮时,这些条目必须加载到购物车表中。 问题是我不知道如何将id_user字段存储到变量中,因此我不必具有以下代码行:

while (resultSet2.next()) 

这使我的代码仅执行一次,因此支架中只有一个条目被加载到购物车表中。这是因为resultSet2在表中找不到更多新用户,因为我已登录到单个用户。如何将支架中的所有数据放入购物车表?

这是功能:

   @RequestMapping("/checkout")
    public void checkout(@RequestParam String username) {
        System.out.println("this is checkout from book controller");
        System.out.println("username is " + username);
        Connection connection = ConnectToDatabase.createConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from cartentries");
            ResultSet resultSet = preparedStatement.executeQuery();

            PreparedStatement preparedStatement2 = connection.prepareStatement("select id_user from user where username='" + username + "'");
            ResultSet resultSet2 = preparedStatement2.executeQuery();
            while (resultSet.next()) {
                while (resultSet2.next()) {
                    PreparedStatement preparedStatement1 = connection.prepareStatement("INSERT INTO cart(id_user,id_cartEntry,totalPrice)VALUES(?,?,?)");
                    preparedStatement1.setInt(1,resultSet2.getInt("id_user"));
                    preparedStatement1.setInt(2, resultSet.getInt("id_cartEntry"));
                    preparedStatement1.setInt(3, resultSet.getInt("totalPrice"));
                    preparedStatement1.executeUpdate();
                }
            }

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

    }

1 个答案:

答案 0 :(得分:0)

我已将您的代码更改为“找到”用户ID(如果找到)。

    try {
        PreparedStatement preparedStatement = connection.prepareStatement("select * from cartentries");
        ResultSet resultSet = preparedStatement.executeQuery();

        PreparedStatement preparedStatement2 = connection.prepareStatement("select id_user from user where username=?");

        preparedStatement2.setString(1, username);
        ResultSet resultSet2 = preparedStatement2.executeQuery();


        PreparedStatement preparedStatement1 = connection.prepareStatement("INSERT INTO cart(id_user,id_cartEntry,totalPrice)VALUES(?,?,?)");

        Integer userId;
        if (resultSet2.next()) {
            // guess it found at least one user
            userId = resultSet2.getInt("id_user");
        }

        if (userId != null) {
            while (resultSet.next()) {
                preparedStatement1.setInt(1, userId);
                preparedStatement1.setInt(2, resultSet.getInt("id_cartEntry"));
                preparedStatement1.setInt(3, resultSet.getInt("totalPrice"));
                preparedStatement1.executeUpdate();
            }
        }


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

我还将您的userid查询更改为使用setString而不是串联。请参见 SQLInjection

也许您可以将这些语句重构为单独的方法,以更好地阅读代码。

PS。不要忘记添加finally子句并关闭连接(或try-with-resources),以防止数据库服务器上发生资源泄漏。