1. Inventory Management System in java - Login Page (JFrame, Mysql Database, Netbeans IDE)
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
- Install NetBeans IDE
- Download and install the latest version of NetBeans IDE from the official website.
- Install JDK
- Ensure that you have the Java Development Kit (JDK) installed. You can download it from the Oracle website.
- 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
- Open NetBeans IDE
- Launch the IDE and select "File" > "New Project."
- Select Project Type
- Choose "Java" under Categories and "Java Application" under Projects.
- Name Your Project
- Enter a name for your project (e.g., InventoryManagementSystem) and click "Finish."
Step 3: Design the Login Page
- Create a New JFrame Form
- Right-click on the "Source Packages" folder > "New" > "Other" > "Swing GUI Forms" > "JFrame Form."
- Name it
LoginPage
.
- 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.
- Two
- Arrange the components using a layout manager for better visual alignment.
- Add the following components to the JFrame:
Step 4: Connect to MySQL Database
-
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.
-
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; }
- In the
Step 5: Implement Login Functionality
- Add Action Listener to Login Button
- Add an action listener to the login button to handle user authentication.
- 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
- Run Your Project
- Click the "Run" button in NetBeans.
- 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!