1. Inventory Management System in java - Login Page (JFrame, Mysql Database, Netbeans IDE)

4 min read 29 days ago
Published on Sep 11, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a comprehensive guide to creating a login page for an Inventory Management System using Java, JFrame, and MySQL database, all within the NetBeans IDE. This project is ideal for those looking to enhance their programming skills and learn about database integration in Java applications.

Step 1: Set Up Your Development Environment

  1. Install NetBeans IDE
    • Download and install the latest version of NetBeans IDE from the official website.
  2. Install JDK
    • Ensure that you have the Java Development Kit (JDK) installed. You can download it from the Oracle website.
  3. Set Up MySQL Database
    • Download and install MySQL Server.
    • Use MySQL Workbench or any other database management tool to create a new database for your project.

Step 2: Create a New Project in NetBeans

  1. Open NetBeans IDE
    • Launch the IDE and select "File" > "New Project."
  2. Select Project Type
    • Choose "Java" under Categories and "Java Application" under Projects.
  3. Name Your Project
    • Enter a name for your project (e.g., InventoryManagementSystem) and click "Finish."

Step 3: Design the Login Page

  1. Create a New JFrame Form
    • Right-click on the "Source Packages" folder > "New" > "Other" > "Swing GUI Forms" > "JFrame Form."
    • Name it LoginPage.
  2. Design the UI Components
    • Add the following components to the JFrame:
      • Two JTextField for username and password.
      • Two JLabel for labels next to the text fields.
      • A JButton for the login action.
    • Arrange the components using a layout manager for better visual alignment.

Step 4: Connect to MySQL Database

  1. Add MySQL Connector/J to Your Project

    • Download the MySQL Connector/J (JDBC driver) from the MySQL website.
    • In NetBeans, right-click on your project > "Properties" > "Libraries" > "Add JAR/Folder" and select the downloaded JAR file.
  2. Write the Database Connection Code

    • In the LoginPage class, create a method to establish a connection to the MySQL database:
    public Connection connectDatabase() {
        Connection conn = null;
        try {
            String url = "jdbc:mysql://localhost:3306/yourDatabaseName";
            String user = "yourUsername";
            String password = "yourPassword";
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    

Step 5: Implement Login Functionality

  1. Add Action Listener to Login Button
    • Add an action listener to the login button to handle user authentication.
  2. Write Authentication Logic
    • In the action listener, retrieve the input from the text fields and perform a query to check the credentials:
    String username = usernameTextField.getText();
    String password = String.valueOf(passwordTextField.getPassword());
    
    Connection conn = connectDatabase();
    String query = "SELECT * FROM users WHERE username=? AND password=?";
    try {
        PreparedStatement pst = conn.prepareStatement(query);
        pst.setString(1, username);
        pst.setString(2, password);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            // Login successful
            JOptionPane.showMessageDialog(null, "Login Successful");
            // Redirect to home page
        } else {
            // Login failed
            JOptionPane.showMessageDialog(null, "Invalid Credentials");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    

Step 6: Test Your Application

  1. Run Your Project
    • Click the "Run" button in NetBeans.
  2. Test Login Functionality
    • Enter valid and invalid credentials to ensure the login functionality works as expected.

Conclusion

You have successfully created a login page for your Inventory Management System using Java, JFrame, and MySQL. This foundational step will allow you to build upon the project and add additional features like user registration, product management, and more. As a next step, consider implementing the home page and other functionalities mentioned in the video series for a complete system. Happy coding!