Order management function of Java warehouse management system
Order management is one of the important functions of the warehouse management system. Through order management, you can purchase, view, modify and delete products in the warehouse. In this article, we will introduce how to use Java to develop the order management function of the warehouse management system and provide specific code examples.
import java.sql.*; public class OrderManagementSystem { private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mydb"; private static final String USERNAME = "root"; private static final String PASSWORD = "password"; public static void addOrder(String productName, int quantity) { try (Connection connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD); PreparedStatement statement = connection.prepareStatement("INSERT INTO orders (product_name, product_quantity) VALUES (?, ?)")) { statement.setString(1, productName); statement.setInt(2, quantity); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public static void viewOrders() { try (Connection connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM orders")) { while (resultSet.next()) { int orderId = resultSet.getInt("order_id"); String productName = resultSet.getString("product_name"); int quantity = resultSet.getInt("product_quantity"); System.out.println("Order ID: " + orderId); System.out.println("Product: " + productName); System.out.println("Quantity: " + quantity); System.out.println(); } } catch (SQLException e) { e.printStackTrace(); } } public static void updateOrder(int orderId, String productName, int quantity) { try (Connection connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD); PreparedStatement statement = connection.prepareStatement("UPDATE orders SET product_name = ?, product_quantity = ? WHERE order_id = ?")) { statement.setString(1, productName); statement.setInt(2, quantity); statement.setInt(3, orderId); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public static void deleteOrder(int orderId) { try (Connection connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD); PreparedStatement statement = connection.prepareStatement("DELETE FROM orders WHERE order_id = ?")) { statement.setInt(1, orderId); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { // 示例代码 // 添加订单 addOrder("商品A", 5); addOrder("商品B", 10); // 查看订单 System.out.println("当前订单:"); viewOrders(); // 修改订单 updateOrder(1, "商品C", 20); // 删除订单 deleteOrder(2); // 再次查看订单 System.out.println("修改后的订单:"); viewOrders(); } }
The above Java code example connects to the MySQL database through JDBC and provides the functions of adding orders, viewing orders, and modifying orders. and the ability to delete orders. In the sample code, we use the try-with-resources statement to ensure that the resources are closed correctly.
It is worth noting that the above code examples are for demonstration purposes only and do not consider the integrity of business logic, exception handling, security, etc. In actual projects, appropriate adjustments and optimizations need to be made based on business needs and scale.
The above is the detailed content of Using Java to develop the order management function of the warehouse management system. For more information, please follow other related articles on the PHP Chinese website!