Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void addOrderToTable(Order order, Connection dbConnection) throws SQLException {
- List<Book> books = order.getBooks();
- String login = order.getUser().getLogin();
- Statement statement = dbConnection.createStatement();
- ResultSet resultSet = statement.executeQuery("SELECT * FROM \"USERS\" WHERE (login) = " + login);
- String addOrder = "INSERT INTO \"ORDERS\" (userId, sum, isPaid) " +
- "VALUES (?, ?, ?)";
- PreparedStatement preparedStatement = dbConnection.prepareStatement(addOrder);
- preparedStatement.setInt(1, resultSet.getInt("id"));
- preparedStatement.setDouble(2, order.getSum());
- preparedStatement.setBoolean(3, false);
- preparedStatement.executeUpdate();
- for (Book book: books) {
- ResultSet bookId = statement.executeQuery("SELECT * FROM \"SHOP_DEPO\" WHERE author = "
- + book.getAuthor() +
- " AND title = " + book.getTitle() +
- " AND publisher = " + book.getPublisher() +
- " AND year = " + book.getYear() +
- " AND pages = " + book.getPages() +
- " AND price = " + book.getPrice());
- ResultSet orderId = statement.executeQuery("SELECT * FROM \"ORDERS\" WHERE" +
- " (userId) = " + resultSet.getInt("id"));
- String addBookId = "INSERT INTO \"ORDERS_POSITIONS\" (orderId, bookId) " +
- "VALUES (?, ?)";
- PreparedStatement addBookIdstatement = dbConnection.prepareStatement(addBookId);
- addBookIdstatement.setInt(1, orderId.getInt("id"));
- addBookIdstatement.setInt(2, bookId.getInt("id"));
- }
- preparedStatement.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement