Java

Java Mysql simple project in IntelliJ IDEA

This Java Myql crud application will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE in IntelliJ IDEA using Mysql Database. IntelliJ IDEA is a very famous editor to writing the Java codes. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.

We will learn how to INSERT, SELECT, UPDATE and DELETE in database by writing code to manage the products table in the database named gbproducts.  products  table consist of following columns Product name,price,qty.

Feature of projects

The system shall be able to record Product details : product name,price,qty.

The system shall be able to retrieve the Product details : product name, price,qty.

Then system shall be able to Edit and Delete the product details : product name, price,qty..

Learn how to make this System Step by step

Jdbc connection in java

Step 1: Download JDK  Click here  follow the steps and install it. Step 2: Download an appropriate jdbc driver Click here in order to connect jdbc and mysql. Establish the database connection

 

Connection con;
PreparedStatement pst;

 public void Connect()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/gbproducts", "root","");
            System.out.println("Success");
        }
        catch (ClassNotFoundException ex)
        {
                ex.printStackTrace();
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

AddRecords

you can use the following code snippet to add the records in to database. paste the code inside the add button

String name,price,qty;

name = txtName.getText();
price = txtPrice.getText();
qty = txtQty.getText();

try {
      pst = con.prepareStatement("insert into products(pname,price,qty)values(?,?,?)");
      pst.setString(1, name);
      pst.setString(2, price);
      pst.setString(3, qty);
      pst.executeUpdate();
      JOptionPane.showMessageDialog(null,"Record Addedddddd!!!!");

      txtName.setText("");
      txtPrice.setText("");
      txtQty.setText("");
      txtName.requestFocus();
   }

   catch (SQLException e1)
   {
      e1.printStackTrace();
   }

Search Records

Enter the Product id on textfield relavent Product information will be displayed on the relavent textfield. textfield event select as keyReleased event

try {

      String pid = txtpid.getText();
      pst = con.prepareStatement("select pname,price,qty from products where pid = ?");
      pst.setString(1, pid);
       ResultSet rs = pst.executeQuery();

       if(rs.next()==true)
           {
               String name = rs.getString(1);
               String price = rs.getString(2);
               String qty = rs.getString(3);

               txtName.setText(name);
               txtPrice.setText(price);
               txtQty.setText(qty);
           }
          else
           {
                txtName.setText("");
                txtPrice.setText("");
                txtQty.setText("");
                JOptionPane.showMessageDialog(null,"Invalid Product ID");

             }
   }

    catch (SQLException ex) 
     {
         ex.printStackTrace();
     }

Edit

you can use the following code snippet to Edit the records.

Paste code inside the Edit button.

 String pid,name,price,qty;

 name = txtName.getText();
 price = txtPrice.getText();
 qty = txtQty.getText();
 pid = txtpid.getText();

   try {
         
         pst = con.prepareStatement("update products set pname = ?,price = ?,qty = ? where pid = ?");
         pst.setString(1, name);
         pst.setString(2, price);
         pst.setString(3, qty);
         pst.setString(4, pid);

         pst.executeUpdate();
         JOptionPane.showMessageDialog(null, "Record Updateee!!!!!");

         txtName.setText("");
         txtPrice.setText("");
         txtQty.setText("");
         txtName.requestFocus();
         txtpid.setText("");
      }

    catch (SQLException e1)
      {

         e1.printStackTrace();
       }

Delete

you can use the following code snippet to Delete the records.

Paste code inside the Delete button.

admin

Recent Posts

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 days 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,…

6 days ago

Best Festivals UK 2025 [Free Guide Included]

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

1 week 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…

1 week 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…

1 week ago

Google Gemini AI Free AI Tool for Students & Creators

Google Gemini AI is among the top talked about developments. What exactly is it? What…

1 week ago