This repository contains a simple inventory management system. It has the following functionalities:
- Adding a new item to the inventory
- Get the details of a specific item
- Get the details of all the items in the inventory
Item getItemDetails (int id);
void addNewItem (Item newItem);
List<Item> getAllItems ();
- Programming Language: Java
- API: Java DataBase Connectivity (JDBC)
- Back-end (Relational Database): MySQL
- Design Pattern: Data Access Object (DAO)
1) Importing Java package
import java.sql.*;
2) Load and Register the Driver
Class.forName("com.mysql.jdbc.Driver");
3) Establishing the connection to the database
connection = DriverManager.getConnection(url, userName, password);
4) Preparing a Statement (or Prepared Statement or Callable Statement)
// Statement
String staticQuery = "select * from inventory";
Statement statement = connection.createStatement();
// Prepared Statement
String dynamicQuery = "select * from inventory where itemID = ?";
PreparedStatement preparedStatement = connection.prepareStatemnt(dynamicQuery);
preparedStatement.setInt(1, id);
5. Executing the Statement
// Statement
ResultSet rs = statement.executeQuery(selectAllQuery);
// PreparedStatement
ResultSet resultSet = preparedStatement.executeQuery();
6. Processing the Result
while (resultSet.next()) {
item.setItemID(resultSet.getInt(1));
item.setItemName(resultSet.getString(2));
item.setItemCompany(resultSet.getString(3));
item.setItemCategory(resultSet.getString(4));
item.setItemPrice(resultSet.getLong(5));
}
7. Terminating the Statement & Connection
statement.close();
preparedStatement.close();
connection.close();
// Note: Use try-catch block to capture the exceptions