Java

JDBC Sales Inventory System Java

This tutorial will teach you JDBC Sales Inventory System step by step.

First Step : The System shall be able to find product information.Enter the relavent product id on the product id textfield to find the product information.relavent product information will be displayed on the below textfields.
select the productid textfield right click and Add event handler->key->keyPressed

import the namespace : import java.sql.*;

Establish the database connection.

Connection con;
PreparedStatement pst;
 
public void Connect()
{
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/wijproducts", "root","");
        }
        catch (ClassNotFoundException ex) 
        {
          ex.printStackTrace();
        }
        catch (SQLException ex) 
        {
          ex.printStackTrace();
        }

}

Database

Paste the code inside the keyReleased Event

public void keyReleased(KeyEvent e) 
{
     String pid = txtpid.getText();
 try
         {
    pst = con.prepareStatement("SELECT * from products where id=?");
    pst.setString(1,pid);
     
     ResultSet rs1=pst.executeQuery();

     if(rs1.next()==false)
  {
    JOptionPane.showMessageDialog(null,"Product ID not found!");
     txtpname.setText("");
     txtprice.setText("");
      txtpid.requestFocus();
   }
  else
  {
    String pname = rs1.getString(2);
    String price = rs1.getString(3);
    String dis = rs1.getString(5);
    txtpname.setText(pname.trim());
     txtprice.setText(price);
     txtdis.setText(dis);
     
    }
               } 
              catch (SQLException e1) 
              { 
  e1.printStackTrace();
        }
}

Paste the code inside the Ok button

public void actionPerformed(ActionEvent e) 
{
 
    String pid = txtpid.getText();
    double qty = Double.parseDouble((txtqty.getText()));
    double price = Double.parseDouble((txtprice.getText()));
    double discount = Double.parseDouble((txtdis.getText()));
          
 try 
     {
 pst = con.prepareStatement("select sum(cast(round((?*? * ?/100),2) as decimal(19,2))) as discount from products where id=?");    
        //select sum(cast(round((price*qty * discount/100),2) as decimal(19,2))) as discount from products where id=?
 pst.setDouble(1,price);
 pst.setDouble(2,qty);
 pst.setDouble(3,discount);
 pst.setString(4,pid);
     
 ResultSet rs1=pst.executeQuery();
 rs1.next();
   
 double discountamount = Double.parseDouble((rs1.getString("discount")));
  
 lbldis.setText(String.valueOf(discountamount));
     
 double subtot =  price * qty - discountamount; 
     
 lblsubtot.setText(String.valueOf(subtot));
 
 }
 catch (SQLException e1) 
{
     
 e1.printStackTrace();
}
}

I have attached the video tutorial below it will help you  to do this  step by step.

 

 

 

 

 

 

 

admin

Recent Posts

Build Simple Water System Calculator in Java Using Swing

If you're just beginning to learn Java GUI programming creating an Water System Calculator is a fantastic project for…

1 week ago

GitHub Copilot vs Microsoft Copilot Best AI Tool to Use in 2025

GitHub is a powerful tool used by teams and developers around the globe. This guide is…

2 weeks ago

Chat with Claude AI Free – Your Super-Smart AI Buddy

It's like having a super-smart buddy that is always there to help you write stories,…

2 weeks ago

Best Festivals UK 2025 [Free Guide Included]

The UK is known for its rich history, diverse culture, and most of all  its…

3 weeks ago

Bank Holidays 2025 UK – Plan Your Perfect Long Weekends

Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…

3 weeks ago

Master Cursor AI Full Guide for Students & Creators

The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…

3 weeks ago